0

I'm getting a syntax error in INSERT INTO statement and I can't figure out why. I've checked several different SO questions that were almost exactly the same as my problem and after changing my code this way and that way it still isn't working.

        var cnnStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", oldDb);
        var cnn = new OleDbConnection(cnnStr);
        cnn.Open();

        //make new access table
        using (OleDbCommand command = new OleDbCommand())
        {
            command.Connection = cnn;
            command.CommandText = String.Format("CREATE TABLE [{0}] ([Tag] string, [Text] string)", newTable + "_Diff");
            try
            {
                command.ExecuteNonQuery();
            }
            catch
            {
                //table already exists
            }
        }

        //fill access table
        using (OleDbCommand command = new OleDbCommand())
        {
            command.Connection = cnn;
            command.CommandText = String.Format("INSERT INTO [{0}] (Tag, Text) VALUES (?, ?)", newTable + "_Diff");
            command.Parameters.Add(new OleDbParameter("Tag", ""));
            command.Parameters.Add(new OleDbParameter("Text", ""));
            for (int i = 0; i < (diffText.Length - 1); i++)
            {
                command.Parameters["Tag"].Value = diffTag[i];
                command.Parameters["Text"].Value = diffText[i];
                command.ExecuteNonQuery();
            }
        }

        cnn.Close();

Creating the table is working so I know there's not a problem with my connection, there's just something it doesn't like about my insert statement.

1 Answer 1

4

In your insert command put text inside a square bracket, "text" is a keyword

command.CommandText = String.Format("INSERT INTO [{0}] (Tag, [Text]) 
                                    VALUES (?, ?)", newTable + "_Diff");

also make sure you are including your values with single quote

values ('?','?')

hope this works

Sign up to request clarification or add additional context in comments.

3 Comments

Ahhhh I thought I had that! I must have taken the brackets out when changing other things. Thanks for taking the time to point that out for me Karthik
I'm trying to select this as the answer but after I click the check mark it takes it away...I'll try again later
yeah.. I can see my reputation going up and down :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.