单元格未更新-VB.net,oledb



我已经和下面的代码斗争了很长时间,就在我认为它有效的时候,它没有,甚至没有给我一个错误。

Dim sql As String = "UPDATE " & table & " SET ArithmeticScore = @score WHERE DateAscending = " & todaysdate
Using connection As New OleDbConnection(conn)
Using command As New OleDbCommand(sql, connection)
connection.Open()
command.Parameters.Add("@score", OleDbType.Decimal).Value = score
MsgBox("score was added = " & score)
command.ExecuteNonQuery()
connection.Close()
End Using
End Using

todaysdate当前保持值27/04/2020作为date

conn保存连接字符串,table保存表名。

消息框输出它的含义,score有一个应该添加到表中的十进制值。

此代码的目的是更新表中的ArithmeticScore列,其中DateAscending = 27/04/2020。程序完成后,我检查数据库,单元格保持空白。为什么会这样?

这是因为执行的查询找不到所需的记录
尝试为DateAscending列的sql参数设置正确的类型。

Dim sql As String = 
$"UPDATE {table} SET ArithmeticScore = @score WHERE DateAscending = @todaysdate"
Using connection As New OleDbConnection(conn)
Using command As New OleDbCommand(sql, connection)
command.Parameters.Add("@score", OleDbType.Decimal).Value = score
command.Parameters.Add("@todaysdate", OleDbType.Date).Value = DateTime.Now.Date
connection.Open()
Dim affectedRows As Integer = command.ExecuteNonQuery()
' Check or display that affectedRows is greater than zero
End Using
End Using

最新更新