尝试将当前日期插入Access数据库会引发INSERT INTO语句的语法错误。
我的数据库包含一个名为Day的字段,其数据类型为Date / Time。
我的插入声明是:
Try
dbConnection.Open()
sqlString = "INSERT INTO table1 (Day) VALUES (@Day)"
dbCommand.CommandText = sqlString
dbCommand.Parameters.AddWithValue("@Day", FormatDateTime(Now, 2))
dbCommand.ExecuteNonQuery()
Finally
dbConnection.Close()
End Try
当我Response.Write(FormatDateTime(Now, 2))
时,它会显示10/29/2013,这应该是可接受的记录。
执行这些操作也会在INSERT INTO语句中抛出语法错误:
Try
dbConnection.Open()
sqlString = "INSERT INTO table1 (Day) VALUES (Now())"
dbCommand.CommandText = sqlString
dbCommand.ExecuteNonQuery()
Finally
dbConnection.Close()
End Try
Try
dbConnection.Open()
sqlString = "INSERT INTO table1 (Day) VALUES (#" & FormatDateTime(Now, 2) & "#)"
dbCommand.CommandText = sqlString
dbCommand.ExecuteNonQuery()
Finally
dbConnection.Close()
End Try
该字段的日期不是可接受的格式吗?
答案 0 :(得分:1)
Day是保留字。将该名称括在方括号中,以避免混淆数据库引擎。
sqlString = "INSERT INTO table1 ([Day]) VALUES (Now())"