简体   繁体   中英

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

I am new in creating an application using Visual Stuido 2010 C# and Microsft Access 2007. I am planning to create an application where the user can add data to the database(MS-Access). But I got an error stating that " Syntax Error (missing operator) In Query Expression ". I really can't find what's the problem with my code.

This is my code in adding data to the database:

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");
            }
        }
    }

This is the screen shot of the error message: 在此处输入图片说明

You need the values part of the insert into statement.

insert into fuel_limit (fuel_limit_code, fuel_limit_description) values (?fuel_limit_code, ?fuel_limit_description)

Also, it looks like your you need to use the description parameter when setting the value in the line right above the ExecuteNonQuery statment.

--- Edited answer --- following will work


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();


--- old answer ---

here is the line that needs correction


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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