ERROR [42000] [Microsoft][ODBC Visual FoxPro Driver]Command contains unrecognized phrase/keyword.代码如下:
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "选择dbf文件";
openFileDialog1.Filter = "dbf文件(*.dbf)|*.dbf";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
conn = new OdbcConnection();
string filePath = Path.GetDirectoryName(openFileDialog1.FileName);
string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + filePath + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO";
conn.ConnectionString = connStr;
conn.Open();
string sql = "select * from " + openFileDialog1.FileName;
OdbcDataAdapter da = new OdbcDataAdapter(sql, conn);
ds = new DataSet();
da.Fill(ds);//此处报错
int col = ds.Tables[0].Columns.Count;
for (int i = 0; i < col; i++)
{
comboBox1.Items.Add(ds.Tables[0].Columns[i].ColumnName);
comboBox2.Items.Add(ds.Tables[0].Columns[i].ColumnName);
}
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
}
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "选择dbf文件";
openFileDialog1.Filter = "dbf文件(*.dbf)|*.dbf";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
conn = new OdbcConnection();
string filePath = Path.GetDirectoryName(openFileDialog1.FileName);
string connStr = @"Driver={Microsoft Visual FoxPro Driver};SourceType=DBF;SourceDB=" + filePath + ";Exclusive=No;NULL=NO;Collate=Machine;BACKGROUNDFETCH=NO;DELETED=NO";
conn.ConnectionString = connStr;
conn.Open();
string sql = "select * from " + openFileDialog1.FileName;
OdbcDataAdapter da = new OdbcDataAdapter(sql, conn);
ds = new DataSet();
da.Fill(ds);//此处报错
int col = ds.Tables[0].Columns.Count;
for (int i = 0; i < col; i++)
{
comboBox1.Items.Add(ds.Tables[0].Columns[i].ColumnName);
comboBox2.Items.Add(ds.Tables[0].Columns[i].ColumnName);
}
comboBox1.SelectedIndex = 0;
comboBox2.SelectedIndex = 0;
}
}
string fileName = openFileDialog1.FileName;
for (int i = openFileDialog1.FileName.Length - 1; i >= 0; i--)
{
if (fileName[i] == '\\')
{
s = i;
break;
}
} if (s != -1)
{
string sql = "select * from " + fileName.Substring(s + 1, fileName.Length - 4 - (s + 1));
//..........
}
SourceDB=c:\windows\abc.dbf 而不是sql语句"select * from c:\windows\abc.dbf"