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

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???
 
J

Jeff Dillon

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
 
L

loquak

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???
 
M

Miha Markic [MVP C#]

Change the @Date to ? in sql statement and possibly put Date in square
brackets.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top