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

标签 c# sql oledb dbf visual-foxpro

某些应用程序用户在使用 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

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

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

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

关于c# - OleDb 异常 : Command contains unrecognized phrase/keyword,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28223540/

相关文章:

sql - 使用 "Native"SQL 连接到数据库是什么意思?

c# - 如何为导入的项目定义命名空间

c# - 使用加密后在终结器线程中获取 "ReleaseHandleFailed"MDA

sql - 有趣的 SQL 测验

mysql - 如果用户被阻止,则跳过 mysql 查询的输出

tsql - 如何解决 OLE DB 提供程序 "Could not find installable ISAM."的 "Microsoft.ACE.OLEDB.12.0"错误

c# - 检查 IIS 的当前最大池大小和池中当前使用的连接数

c# - 在 C# 中将字节数组转换为短数组

java - 通过 PHP 插入 SQL 数据库

c# - 使用 Microsoft Jet OLEDB 对 CSV 的列限制