插入记录时数据类型不匹配
Data type mismatch when inserting record
我有这段代码用于将记录插入到 Access table。
LastUpdated
字段在数据库级别定义为 Date/Time
。插入时失败并给出错误
Data type mismatch in criteria expression
我正在使用参数化查询来避免格式化值的问题,这很奇怪,因为我有相同的代码(具有更多参数)在另一个 table 上插入记录,LastUpdated
以相同的方式定义并且工作正常。
有什么想法吗?
SqlQuery = "INSERT INTO History (ActivityID, LastUpdated) VALUES (@p1,@p2)"
With sqlcommand
.CommandText = SqlQuery
.Connection = SQLConnection
.Parameters.AddWithValue("@p1", IDAct)
.Parameters.AddWithValue("@p2", DateTime.Today)
End With
result = sqlcommand.ExecuteNonQuery()
If (result = 1) Then
LabelWarning.Text = "Activity filled"
LabelWarning.BackColor = Color.ForestGreen
LabelWarning.Visible = True
ButtonSave.Visible = False
ButtonBack.Visible = False
ButtonOK.Visible = True
BlockControls()
End If
问题可能与参数占位符有关。
此 MSDN 文档声明 OleDbCommand 不支持命名参数(仅位置),正确的占位符应为“?”而不是“@p1”。
https://msdn.microsoft.com/en-us/library/yy6y35y8%28v=vs.110%29.aspx
编辑
在评论中发现占位符不必如此严格地遵守 doc 语法。只有顺序必须绝对保留。
不过,显式声明参数类型似乎可以解决问题:
.Parameters.Add("@p2", OleDbType.DBTimeStamp)
.Parameters("@p2").Value = DateTime.Today
我有这段代码用于将记录插入到 Access table。
LastUpdated
字段在数据库级别定义为 Date/Time
。插入时失败并给出错误
Data type mismatch in criteria expression
我正在使用参数化查询来避免格式化值的问题,这很奇怪,因为我有相同的代码(具有更多参数)在另一个 table 上插入记录,LastUpdated
以相同的方式定义并且工作正常。
有什么想法吗?
SqlQuery = "INSERT INTO History (ActivityID, LastUpdated) VALUES (@p1,@p2)"
With sqlcommand
.CommandText = SqlQuery
.Connection = SQLConnection
.Parameters.AddWithValue("@p1", IDAct)
.Parameters.AddWithValue("@p2", DateTime.Today)
End With
result = sqlcommand.ExecuteNonQuery()
If (result = 1) Then
LabelWarning.Text = "Activity filled"
LabelWarning.BackColor = Color.ForestGreen
LabelWarning.Visible = True
ButtonSave.Visible = False
ButtonBack.Visible = False
ButtonOK.Visible = True
BlockControls()
End If
问题可能与参数占位符有关。
此 MSDN 文档声明 OleDbCommand 不支持命名参数(仅位置),正确的占位符应为“?”而不是“@p1”。
https://msdn.microsoft.com/en-us/library/yy6y35y8%28v=vs.110%29.aspx
编辑
在评论中发现占位符不必如此严格地遵守 doc 语法。只有顺序必须绝对保留。
不过,显式声明参数类型似乎可以解决问题:
.Parameters.Add("@p2", OleDbType.DBTimeStamp)
.Parameters("@p2").Value = DateTime.Today