Skip to main content
 首页 » 编程设计

c#之OleDb 异常 : Command contains unrecognized phrase/keyword

2024年02月06日36www_RR

某些应用程序用户在使用 VisualFoxPro 9.0 驱动程序从 DBF 文件填充数据集时收到异常。我似乎无法弄清楚发生了什么,因为错误总是发生在某些用户身上,从未发生在其他用户身上,有时(根据 .DBF 文件的位置)发生在其他用户身上。我确保所有用户都安装了 visual fox pro 9.0 驱动程序。这是导致问题的代码(leieLoc 是 DBF 文件的位置):

string constr = "Provider=VFPOLEDB.1;Data Source=" + this.leieLoc + ";"; 
OleDbConnection con = new OleDbConnection(constr); 
string sql = "select * from " + this.leieLoc + ";"; 
OleDbCommand cmd = new OleDbCommand(sql, con); 
con.Open(); 
DataSet ds = new DataSet(); 
OleDbDataAdapter da = new OleDbDataAdapter(cmd); 
da.Fill(ds); 
da.Dispose(); 
ds.Dispose(); 
cmd.Dispose(); 
con.Dispose(); 
con.Close(); 

我确实希望我能够更改数据库的格式,但是我不能,因为它是由第三方发布的。

错误栈跟踪如下:

System.Data.OleDb.OleDbException (0x80040E14): Command contains unrecognized phrase/keyword. 
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) 
    at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) 
    at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) 
    at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) 
    at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) 
    at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) 
    at System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) 
    at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 
    at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 
    at System.Data.Common.DbDataAdapter.Fill(DataSet dataSet) 
    at ExclusionSearcher.Searching.ThreadProcesser.searchLeie(List`1 names) 

任何帮助将不胜感激;我不知道是什么导致了这里的问题。

请您参考如下方法:

你有问题

string sql = "select * from " + this.leieLoc + ";"; 
string constr = "Provider=VFPOLEDB.1;Data Source=" + this.leieLoc + ";"; 

首先你的表和数据源不能相同。如果错误是Command contains unrecognized phrase/keyword 我会假设连接字符串是正确的。这意味着您应该在 sql 字符串中修复您的 tableName。

也不需要在连接上调用Close 和Dispose。只有关闭就够了!无需Dispose DataSet。您也可以使用 using 而不是 Dispose,因为在您的代码中,如果发生异常,资源将不会被 Dispose。如果想直接使用Dispose,需要这样写:

try 
{ 
    conn.Open() 
} 
catch(Exception) 
{ 
    throw; 
} 
finnaly 
{ 
    conn.Close(); 
} 

在这种情况下,您保证连接将被关闭。这也应该为适配器完成。