繁体   English   中英

OleDbException未处理:查询表达式中的语法错误(缺少运算符)

[英]OleDbException was unhandled: Syntax Error (missing operator) In Query Expression

我是使用Visual Stuido 2010 C#和Microsft Access 2007创建应用程序的新手。我打算创建一个应用程序,用户可以在其中向数据库(MS-Access)添加数据。 但是我收到一条错误消息,指出“ 查询表达式中的语法错误(缺少运算符) ”。 我真的找不到我的代码有什么问题。

这是我向数据库添加数据的代码:

private void buttonSaveFuelLimit_Click(object sender, EventArgs e)
    {
        string MyConString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\KKKKK\Documents\Visual Studio 2010\Projects\Trial\Trial\gxi.accdb";
        OleDbConnection connection = new OleDbConnection(MyConString);
        OleDbCommand command = connection.CreateCommand();
        command.Connection = connection;
        using (OleDbConnection conn = new OleDbConnection(MyConString))
        {
            connection.Open();
            using (OleDbCommand com = connection.CreateCommand())
            {
                command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?fuel_limit_code, ?fuel_limit_description)";
                command.Parameters.Add(new OleDbParameter("?fuel_limit_code", OleDbType.VarChar));
                command.Parameters.Add(new OleDbParameter("?fuel_limit_description", OleDbType.VarChar));
                command.Parameters["?fuel_limit_code"].Value = textBoxFuelLimitCode.Text;
                command.Parameters["?fuel_limit_description"].Value = textBoxFuelLimitDesc.Text;
                command.ExecuteNonQuery();
                MessageBox.Show("Data Saved");
            }
        }
    }

这是错误消息的屏幕截图: 在此处输入图片说明

您需要插入到语句中的值部分。

将值插入(fuel_limit_code,fuel_limit_description)值(?fuel_limit_code,?fuel_limit_description)

同样,当您在ExecuteNonQuery语句上方的行中设置值时,您似乎需要使用description参数。

---编辑的答案---以下将起作用


command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?, ?)";
command.Parameters.AddWithValue(new OleDbParameter("@fuel_limit_code",textBoxFuelLimitCode.Text));
command.Parameters.AddWithValue(new OleDbParameter("@fuel_limit_desc", textBoxFuelLimitDesc.Text));
command.ExecuteNonQuery();


---旧答案-

这是需要纠正的行


command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description)";
//should ne
command.CommandText = "insert into fuel_limit(fuel_limit_code, fuel_limit_description) values(?,?)";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM