C#插入访问引发异常

C#插入访问引发异常,c#,sql,database,ms-access,oledb,C#,Sql,Database,Ms Access,Oledb,我有插入Access数据库的代码 这段代码通常是有效的,但我有一个表不起作用,我不明白为什么 这是我得到的例外: Syntax error in INSERT INTO statement. 代码如下: if (connection == null) Connect(); command = new OleDbCommand(SQL); command.Connection = connection; try { connection.Open(); command.Exec

我有插入Access数据库的代码

这段代码通常是有效的,但我有一个表不起作用,我不明白为什么

这是我得到的例外:

Syntax error in INSERT INTO statement.
代码如下:

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

try
{
    connection.Open();
    command.ExecuteNonQuery();
}
catch (Exception ex) { }
finally { connection.Close(); }
这是SQL字符串:

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

trackingDateTimeID Number
trackingID Number
dateTime Date/Time
info Text
我错过了什么


谢谢,

您正在尝试插入用#而不是单引号括起来的日期

换成这个

插入TrackingDateTimes(trackingDateTimeID,trackingID,dateTime,info)值(1,0,'.'02/05/2017 21:37:00,'')


编辑:我错了,史蒂夫是对的。

您试图插入的日期用#而不是单引号括起来

换成这个

插入TrackingDateTimes(trackingDateTimeID,trackingID,dateTime,info)值(1,0,'.'02/05/2017 21:37:00,'')


编辑:我错了,史蒂夫是对的。

将列命名为保留关键字并不是一个好主意

如果你真的想用这个名字(我建议改一下),那么你需要用方括号括住这个名字

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

将列命名为保留关键字并不是一个好主意

如果你真的想用这个名字(我建议改一下),那么你需要用方括号括住这个名字

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

首先,您应该使用参数化查询,而不是内联插入变量。另一件事是,
dateTime
在MS-Access中是一个保留字,可能需要使用backticks
[ReservedWord]
进行转义

这将为您重写查询提供一个良好的开端

// string qry = "Insert INTO TrackingsDateTimes ( trackingDateTimeID, trackingID, `dateTime`, info) VALUES(@trackingDateTimeID, #trackingID, @dateTime, @info)"
string qry = "Insert INTO TrackingsDateTimes ( trackingDateTimeID, trackingID, [dateTime], info) VALUES(@trackingDateTimeID, #trackingID, @dateTime, @info)"

if (connection == null) Connect();
command = new OleDbCommand(SQL);
command.Connection = connection;
command.Parameters.AddWithValue("@trackingDateTimeID", (int)1);
command.Parameters.AddWithValue("@trackingID", (int)0);
command.Parameters.AddWithValue("@dateTime", DateTime.Parse("2/05/2017 21:37:00");
command.Parameters.AddWithValue("@info", string.Empty);

首先,您应该使用参数化查询,而不是内联插入变量。另一件事是,
dateTime
在MS-Access中是一个保留字,可能需要使用backticks
[ReservedWord]
进行转义

这将为您重写查询提供一个良好的开端

// string qry = "Insert INTO TrackingsDateTimes ( trackingDateTimeID, trackingID, `dateTime`, info) VALUES(@trackingDateTimeID, #trackingID, @dateTime, @info)"
string qry = "Insert INTO TrackingsDateTimes ( trackingDateTimeID, trackingID, [dateTime], info) VALUES(@trackingDateTimeID, #trackingID, @dateTime, @info)"

if (connection == null) Connect();
command = new OleDbCommand(SQL);
command.Connection = connection;
command.Parameters.AddWithValue("@trackingDateTimeID", (int)1);
command.Parameters.AddWithValue("@trackingID", (int)0);
command.Parameters.AddWithValue("@dateTime", DateTime.Parse("2/05/2017 21:37:00");
command.Parameters.AddWithValue("@info", string.Empty);

日期必须在单引号上…@Gusman在Access中,这不是真的。Access中的日期文字可以用八进制(#)括起来。为了便于将来参考,请将查询粘贴到Access查询设计器中,并从中进行调试。日期必须在单引号上…@Gusman in Access,这不是真的。Access中的日期文字可以用八进制字符(#)包围。为了便于将来参考,请将查询粘贴到Access查询设计器中,并从中进行调试。这将导致类型不匹配错误Access accepts constant DateTime,日期前后的格式为MM/dd/yyyy@Steve我不知道这是Access的要求,是否仅使用过T-SQLConstant日期时间值?对这是始终使用参数的另一个原因。这将导致类型不匹配错误,Access接受常数DateTime,在日期前后以MM/dd格式显示/yyyy@Steve我没有意识到这是Access中的要求,只使用过t-SQLConstant日期时间值?对这是始终使用参数的另一个原因当我同意参数概念(尽管海报使用常量值)时,反勾号是访问的语法错误。当我同意参数概念(尽管海报使用常量值)时,反勾号是访问的语法错误。谢谢,我不知道!谢谢,我不知道!