0

Can some one please help me to resolve the issue ?

I am getting the syntax error while running this code.

OleDbConnection cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\CADFileLogger.accdb");
string cmdText = "INSERT INTO [tbl_Logger] (Username,FullFileName,DateOfAction,EventMode,ShortFileName) VALUES (@a,@b,@c,@d,@e)";

cn.Open();

OleDbCommand cmd = new OleDbCommand(cmdText, cn);
cmd.Parameters.Add("@a", OleDbType.WChar).Value = username;
cmd.Parameters.Add("@b", OleDbType.WChar).Value = fullfilename;
cmd.Parameters.Add("@c ", OleDbType.DBTimeStamp).Value = now;
cmd.Parameters.Add("@d ", OleDbType.WChar).Value = mode;
cmd.Parameters.Add("@e ", OleDbType.WChar).Value = filename;

cmd.ExecuteNonQuery();

cn.Close();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Could it be that `Username` is a reserved word or function? I'm just guessing really. – Lasse V. Karlsen May 12 '22 at 07:35
  • 1
    This is a great example of where the *precise* error details - and the fact that it was a compilation error rather than an exception - would have been useful to include in the question. It's entirely feasible for *SQL* to have a syntax error, but knowing that it was a *compilation* error would have ruled that out as being the immediate problem. – Jon Skeet May 12 '22 at 07:41

1 Answers1

1

The problem is the backslash in the data source:

OleDbConnection cn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\CADFileLogger.accdb");

Either put an @ in front of the connection string or escape the backspace:

OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=J:\CADFileLogger.accdb");
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
diiN__________
  • 7,393
  • 6
  • 42
  • 69