Syntax error in INSERT INTO statement. System.Data

2019-07-15 17:42发布

问题:

Hi I am creating basic form in Visual Studio 2012 Express. I am using Microsoft Access 2012 as database. My problem is when I press submit button I nothing happens.

Syntax error in INSERT INTO statement. System.Data.OleDb.OleDbErrorCollection

My code is given below. Please help me to resolve this issue.

protected void Button1_Click(object sender, EventArgs e)
{
    string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\admin\Desktop\del\SHAFI\db.accdb";
    OleDbConnection con = new OleDbConnection(conString);
    OleDbCommand cmd = con.CreateCommand();
    string text = "INSERT INTO TEST (Number, Amount) VALUES (?, ?)";
    cmd.CommandText = text;
    try
    {
        con.Open();
        cmd.Parameters.AddWithValue("@Number", txtAmount.Text);
        cmd.Parameters.AddWithValue("@Amount", txtOrder.Text);
        cmd.ExecuteNonQuery();
    }
    catch (OleDbException ex)
    {
        txtAmount.Text = "Sorry";
        Response.Write(ex.Message.ToString() + "<br />" + ex.Errors.GetType());
    }
}

回答1:

OleDbCommand does not support named parameters. You have to add the parameters in the order you want them.

//Code above
con.Open();
cmd.Parameters.Add(txtAmount.Text);
cmd.Parameters.Add(txtOrder.Text);
cmd.ExecuteNonQuery();
//Code below


回答2:

Your code is correct, apart from the lacking of using statement around disposable objects, but the real problem is the word NUMBER. It is a reserved keyword for MSAccess.
Use it enclosed in square brackets.

string text = "INSERT INTO TEST ([Number], Amount) VALUES (?, ?)";

However, if it is still possible, I really suggest you to change that column name. This problem will become very annoying.

protected void Button1_Click(object sender, EventArgs e)
{
    string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\admin\Desktop\del\SHAFI\db.accdb";
    using(OleDbConnection con = new OleDbConnection(conString))
    using(OleDbCommand cmd = con.CreateCommand())
    {
        string text = "INSERT INTO TEST ([Number], Amount) VALUES (?, ?)";
        cmd.CommandText = text;
        try
        {
            con.Open();
            cmd.Parameters.AddWithValue("@Number", txtAmount.Text);
            cmd.Parameters.AddWithValue("@Amount", txtOrder.Text);
            cmd.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            txtAmount.Text = "Sorry";
            Response.Write(ex.Message.ToString() + "<br />" + ex.Errors.GetType());
        }
    }


回答3:

You are using @Number and @Amount variables for Number and Amount but not writing these values in query.

? used in java not in asp.net(c#). so you are mixing these two.

protected void Button1_Click(object sender, EventArgs e)
{
    string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\admin\Desktop\del\SHAFI\db.accdb";
    OleDbConnection con = new OleDbConnection(conString);
    OleDbCommand cmd = con.CreateCommand();
    string text = "INSERT INTO TEST (Number, Amount) VALUES (@Number, @Amount)";
    cmd.CommandText = text;
    try
    {
        con.Open();
        cmd.Parameters.AddWithValue("@Number", txtAmount.Text);
        cmd.Parameters.AddWithValue("@Amount", txtOrder.Text);
        cmd.ExecuteNonQuery();
    }
    catch (OleDbException ex)
    {
        txtAmount.Text = "Sorry";
        Response.Write(ex.Message.ToString() + "<br />" + ex.Errors.GetType());
    }
}


回答4:

You should use:

string text = "INSERT INTO TEST (Number, Amount) VALUES (@Number, @Amount)";

instead of:

INSERT INTO TEST (Number, Amount) VALUES (?, ?)