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

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

Post by loqua » Thu, 02 Dec 2004 04:20:32


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

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

Post by Jeff Dillo » Thu, 02 Dec 2004 04:30:40

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


variable
Date

 
 
 

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

Post by loqua » Thu, 02 Dec 2004 04:40:36

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 :-)
 
 
 

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

Post by Miha Marki » Thu, 02 Dec 2004 04:41:33

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

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
SLODUG - Slovene Developer Users Group
www.rthand.com