I write a string for INSERT to mdb file in c#, but error “syntax error in insert into statment” catched in code.
I test string directly in Microsoft Access 2007 and query inserted with no error.
string qu = "INSERT INTO Reports ( docid, biopsy, normal, section, subsection, title, items, [text] ) VALUES (21, False, False, 'Recommendation', 'a', 'Injection', 'a', 'a');";
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataSet ds = new DataSet();
OleDbConnection con = Model.getDataConnection();
cmd.Connection = con;
cmd.CommandText = qu;
con.Open();
try
{
cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("data error!\n" + ex.Message);
return false;
}
The original string that shows the correct bracketing, prior to an unfortunate edit.
string qu = "INSERT INTO Reports ( docid, biopsy, normal, [section], subsection, title, items, [text] ) VALUES (21, False, False, 'Recommendation', '', 'Injection', '', '');";
Reports, section, and text are all reserved words. Enclose them in brackets in your SQL statement.