C# VFP OLEDB 查询抛出 "Command contains unrecognized phrase/keyword"

C# VFP OLEDB query throwing "Command contains unrecognized phrase/keyword"

我有一个 C# 应用程序打算根据在另一个 table 中找到的值更新 table 中的一个字段。使用以下内容:

listComm.CommandText = "update [c-disc] inner join [c-info] " +
 "on [c-info].keys = [c-disc].cd_key set [c-disc].cd_distric = ? " +
 "where [c-disc].cd_tax = ? and [c-info].ci_region = ?";

并在其下方的 foreach 循环中:

string region = line.Substring(0, 4).PadRight(14);
string taxable = "Y";
string district = line.Substring(5, 4).PadLeft(4);
listComm.Parameters.Add(new OleDbParameter("?", district));
listComm.Parameters.Add(new OleDbParameter("?", taxable));
listComm.Parameters.Add(new OleDbParameter("?", region));

try {
    listComm.ExecuteNonQuery();
    listComm.Parameters.Clear();
} catch (Exception x) {
    setStatusText("fatal error: " + x.Message.ToString();
}

我正在 "Command contains unrecognized phrase/keyword"。当我插入适当的值代替“?”时,在 MS Access 中使用相同的查询效果很好占位符。在 Visual Studio 中,使用断点我看到一切都正常 - 连接已打开并且参数值符合预期。我有另一个类似的程序,但只针对单个 table。我一辈子都弄不明白这个查询有什么问题。

谁设计了那个系统,听起来对VFP的了解不多。 VFP 不完全与 ANSI SQL 兼容,不仅有一些命名规则。您的设计师将 table 命名为带有破折号?我记得在文档中有警告。无论如何,您仍然可以继续希望,cd_key 和 cd_tax 字段仅在 'c-disc' table 中(否则您需要一些解决方法)。

using (OleDbConnection con = new OleDbConnection(@"Provider=VFPOLEDB;Data Source=c:\MyDataFolder"))
{
    var sql = @"update [c-disc] 
set cd_distric = ? 
from [c-info] ci 
WHERE ci.keys = cd_key AND 
    cd_tax = ? and ci.ci_region = ?";

    var listComm = new OleDbCommand(sql, con);

    listComm.Parameters.Add("dst", OleDbType.Char);
    listComm.Parameters.Add("tx", OleDbType.Char);
    listComm.Parameters.Add("reg", OleDbType.Char);

    string taxable = "Y";

    listComm.Parameters["tx"].Value = taxable; // constant?

    con.Open();

    // Loop here
    // {

    // These paddings do not smell good
    string region = line.Substring(0, 4).PadRight(14); 
    string district = line.Substring(5, 4).PadLeft(4);

    listComm.Parameters["dst"].Value = district;
    listComm.Parameters["reg"].Value = region;
    try
    {
        listComm.ExecuteNonQuery();
    }
    catch (Exception x)
    {
        setStatusText("fatal error: " + x.Message.ToString());
    }
    // loop ends here
    // }
    con.Close();
}