Getting syntax error in INSERT INTO statement

2019-01-12 13:37发布

问题:

So I'm receiving this problem, I tried everything. Kinda stuck

My access database design:

ID - AutoNumber
Data - ShortText

And the insert command:

OleDbCommand command = new OleDbCommand();

string sql = "INSERT INTO Table (Data) VALUES (@Data)";
OleDbCommand cmd = new OleDbCommand(sql, connection);
cmd.Parameters.AddWithValue("@Data", "Test");

connection.Open();

try
{
    cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
    MessageBox.Show("err: " + ex);
    throw;
}

connection.Close();

This might be dumb but I'm really lost and I have no idea what to do.

回答1:

The TABLE is a reserved keyword so you should change it's name or you should enclose it in square brackets like [Table]:

string sql = "INSERT INTO [Table] (Data) VALUES (@Data)";


标签: c# ms-access