|
楼主 |
发表于 2013-7-7 13:19:21
|
显示全部楼层
经过一天的折腾
发现dbf表是不是默认放在某个文件夹下,该文件夹就可以视为数据库,而里面的文件就可以看做是表
按照这个思路我重写了下代码
sql语句我在VFP重查询是通过的,不过C#提示我语法from附件有错。
还请高手帮我看下- public partial class Form2 : Form
- {
- string dbfPath = string.Empty;
- public Form2()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (string.IsNullOrEmpty(dbfPath))
- {
- MessageBox.Show("还没选择文件夹!");
- return;
- }
- /*
- if (string.IsNullOrEmpty(comboBox1.Text))
- {
- MessageBox.Show("没有选择数据文件");
- return;
- }
- */
- string connectString = string.Format(
- "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=dBASE IV;User ID=Admin;Password=;"
- , dbfPath);
- using (OleDbConnection connection = new OleDbConnection(connectString))
- {
- DataSet ds = new DataSet();
- try
- {
- connection.Open();
- //OleDbDataAdapter command = new OleDbDataAdapter("select * from " + comboBox1.Text, connection);
- //string str = @"select t_tdd.lqzy,t_jhk.zydm,t_jhk.zymc from t_tdd.dbf,t_jhk.dbf where t_tdd.lqzy = t_jhk.zydh";
- string str = @"select a.Ksh,a.Xm,a.Sfzh,a.Lqzy,a.Tdcj,b.Zydm,b.Zymc from T_TDD a join T_jhk b on a.Lqzy = b.Zydh";
- OleDbDataAdapter command = new OleDbDataAdapter(str, connection);
- //OleDbDataAdapter command = new OleDbDataAdapter(@"select a.Ksh,a.Xm,a.Sfzh,a.Lqzy,a.Tdcj,b.Zydh,b.Zymc from " + comboBox1.Text + " a join " + comboBox2.Text + " b on a.Lqzy = b.Zydh", connection);
- command.Fill(ds, "ds");
- MessageBox.Show(ds.Tables[0].Rows.Count.ToString());
- }
- catch (Exception ex)
- {
- MessageBox.Show(string.Format("error:{0}", ex.Message));
- }
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- if (folderBrowserDialog1.ShowDialog() == DialogResult.Cancel)
- return;
- dbfPath = folderBrowserDialog1.SelectedPath;
- DirectoryInfo di = new DirectoryInfo(dbfPath);
- /*
- comboBox1.Items.Clear();
- foreach (FileInfo fi in di.GetFiles())
- {
- if (fi.Extension.ToLower() == ".dbf")
- this.comboBox1.Items.Add(fi.Name.Substring(0, fi.Name.LastIndexOf(fi.Extension)));
- this.comboBox2.Items.Add(fi.Name.Substring(0, fi.Name.LastIndexOf(fi.Extension)));
- }
- if (comboBox1.Items.Count == 0 | comboBox2.Items.Count == 0)
- {
- MessageBox.Show("此文件夹中没有数据文件,请确认");
- }
- else
- {
- comboBox1.SelectedIndex = 0;
- }
- */
- }
- }
复制代码 |
|