我正在尝试更新记录,但收到错误:运营商' ='未定义类型'日期'并输入'整数'。我该如何解决这个问题?
If Me.txt_Team.Tag = 0 Then
sSQL = "INSERT INTO FixtureandResultsDatabase (Fixture_Date, Team, Ground, Score)"
sSQL = sSQL & " VALUES(?, ?, ?, ?)"
cmd.CommandText = sSQL
Else
sSQL = "UPDATE FixtureandResultsDatabase set Fixture_Date = @Fixture_Date, Team = @Team, Ground = @Ground, Score = @Score WHERE ID = @id"
cmd.CommandText = sSQL
End If
cmd.Parameters.Add("@Fixture_Date", OleDbType.Date).Value = Me.dtp_Date.Text
cmd.Parameters.Add("@Team", OleDbType.VarChar).Value = Me.txt_Team.Text
cmd.Parameters.Add("@Ground", OleDbType.VarChar).Value = Me.cb_Ground.Text
cmd.Parameters.Add("@Score", OleDbType.VarChar).Value = Me.txt_Score.Text
If Me.txt_Team.Tag <> 0 Then
cmd.Parameters.Add("@ID", OleDbType.Numeric).Value = Me.txt_Team.Tag
End If
cmd.ExecuteNonQuery()
If Me.txt_Team.Tag = 0 Then
cmd.Parameters.Clear()
cmd.CommandText = "Select @@Identity"
Me.txt_Team.Tag = cmd.ExecuteScalar()
End If
MsgBox("Database has been updated.")
conn.Close()
答案 0 :(得分:0)
像这样修复
而不是
cmd.Parameters.Add("@Fixture_Date", OleDbType.Date).Value = Me.dtp_Date.Text
做
cmd.Parameters.Add("@Fixture_Date", OleDbType.Date).Value = Me.dtp_Date.Value
您应该发布表格结构[sp_help 'tableName']
。您显然有日期字段,但是您尝试将value
[object]设置为字符串 - Me.dtp_Date.Text
。
另一方面 - 尽快摆脱OleDB - 使用SqlClient
。 oleDb即将退役。此外,您可以在更新中使用Output Inserted.Id
,并使用ExecuteScalar
代替ExecuteNonQuery
。它会在一个语句中返回您的身份 - 而不是Select @@Identity
。