在Access数据库中插入日期会出现语法错误

时间:2022-06-01 20:16:37

Trying to insert the current date into my Access database throws a syntax error for the INSERT INTO statement.

尝试将当前日期插入到我的Access数据库中会引发INSERT INTO语句的语法错误。

My database contains a field named Day which has the data type Date/Time.

我的数据库包含一个名为Day的字段,其数据类型为Date / Time。

My insert statement is:

我的插入声明是:

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

When I do Response.Write(FormatDateTime(Now, 2)) it displays 10/29/2013, which should be an acceptable record.

当我做Response.Write(FormatDateTime(Now,2))时,它显示10/29/2013,这应该是一个可接受的记录。

Doing these also throw a syntax error in the INSERT INTO statement:

执行这些操作也会在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

Are the dates not in an acceptable format for the field?

该字段的日期不是可接受的格式吗?

1 个解决方案

#1


1  

Day is a reserved word. Enclose that name in square brackets to avoid confusing the db engine.

日是一个保留字。将该名称括在方括号中以避免混淆数据库引擎。

sqlString = "INSERT INTO table1 ([Day]) VALUES (Now())"

#1


1  

Day is a reserved word. Enclose that name in square brackets to avoid confusing the db engine.

日是一个保留字。将该名称括在方括号中以避免混淆数据库引擎。

sqlString = "INSERT INTO table1 ([Day]) VALUES (Now())"