【问题标题】:OleDbException was unhandled: Syntax Error (missing operator) In Query ExpressionOleDbException 未处理:查询表达式中的语法错误(缺少运算符)
【发布时间】:2011-11-24 07:09:09
【问题描述】:

我是使用 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");
            }
        }
    }

这是错误消息的屏幕截图:

【问题讨论】:

    标签: c# database visual-studio-2010 ms-access-2007 oledb


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢。我已经添加了代码,但出现了一个新错误。您认为我的代码有什么问题?
    【解决方案2】:

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

    
    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(?,?)";
    

    【讨论】:

    • 我已经在查询中添加了值,但出现了新错误。这可能是什么问题。谢谢
    • 当我使用你的代码时出错(方法'AddWithValue'没有重载需要1个参数)。但是当我用'Add'更改'AddWithValue'时,它只能完美运行。只使用“添加”可以吗?非常感谢。
    • 是的,您也可以使用 Add,有时您会收到警告,因为 Add(paramName, value) 已被弃用,但它会正常工作。
    • 在使用上面的代码时,我在哪里可以指定我将在表单中输入的数据类型?
    • 你不能,这就是它被弃用的原因,如果你在代码中编辑值(?,?),并在添加参数时使用@,它会起作用
    猜你喜欢
    • 2020-01-14
    • 1970-01-01
    • 2018-09-02
    • 2018-01-29
    • 2012-11-14
    相关资源
    最近更新 更多