Search code examples
c#sqldatabasems-accessoledb

C# Insert Into Access Throwing Exception


I have code that insert into Access Database.

This code usually works, but i have one table that doesn't work and i can't understand why.

This is the exception i get:

Syntax error in INSERT INTO statement.

this is the CODE:

if (connection == null) Connect();
command = new OleDbCommand(SQL);
command.Connection = connection;

try
{
    connection.Open();
    command.ExecuteNonQuery();
}
catch (Exception ex) { }
finally { connection.Close(); }

this is the SQL string:

Insert INTO TrackingsDateTimes (trackingDateTimeID, trackingID, dateTime, info) VALUES(1, 0, #02/05/2017 21:37:00#, '')

this is the table TrackingsDateTimes:

trackingDateTimeID Number
trackingID Number
dateTime Date/Time
info Text

What am i missing?

Thanks,


Solution

  • Not really a good idea to have a column named as a reserved keyword.

    DATETIME is reserved

    If you really want to use that name (I suggest to change it) then you need square brackets around that name

    Insert INTO TrackingsDateTimes (trackingDateTimeID, trackingID, [dateTime], info) VALUES (.....)