Very simple INSERT INTO with a DateTime parameter -> "Syntax Error in INSERT INTO statement"

  • Thread starter Thread starter loquak
  • Start date Start date
L

loquak

Hello.
The following very simple code does not seem to accept the DateTime variable
as a parameter - why?
The table (MS Access db) here contains only a primary key and a column Date
of type date/time.

connection.Open();
transaction = connection.BeginTransaction();

commandString = "INSERT INTO [Events] (Date) Values(@Date)";

command = connection.CreateCommand();
command.CommandText = commandString;
command.Transaction = transaction;

OleDbParameter param_Date = new OleDbParameter("@Date",
OleDbType.DBTimeStamp); // Also tried DBTime and DBDate
param_Date.Value = date; // date == DateTime
command.Parameters.Add(param_Date);

// Execute command
command.ExecuteNonQuery(); // Syntax error in INSERT INTO statement???
 
Is Date your fieldname? Not always a good choice to use reserved words.
Does the INSERT statement work in Access?

Shouldn't it be [Events].[Date] ?

Have you tried Text datatype?

Jeff
 
Thanks for the quick response
Damn I feel dumb... I'm not used to using those brackets, because the
statements usually work well without them, but not in this case - it worked
after adding those brackets :-)

Jeff Dillon said:
Is Date your fieldname? Not always a good choice to use reserved words.
Does the INSERT statement work in Access?

Shouldn't it be [Events].[Date] ?

Have you tried Text datatype?

Jeff
loquak said:
Hello.
The following very simple code does not seem to accept the DateTime variable
as a parameter - why?
The table (MS Access db) here contains only a primary key and a column Date
of type date/time.

connection.Open();
transaction = connection.BeginTransaction();

commandString = "INSERT INTO [Events] (Date) Values(@Date)";

command = connection.CreateCommand();
command.CommandText = commandString;
command.Transaction = transaction;

OleDbParameter param_Date = new OleDbParameter("@Date",
OleDbType.DBTimeStamp); // Also tried DBTime and DBDate
param_Date.Value = date; // date == DateTime
command.Parameters.Add(param_Date);

// Execute command
command.ExecuteNonQuery(); // Syntax error in INSERT INTO statement???
 
Change the @Date to ? in sql statement and possibly put Date in square
brackets.
 
Back
Top