首页 > 解决方案 > 单元格未更新 - 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/2020date

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

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

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

标签: vb.netoledb

解决方案


这是因为执行的查询找不到所需的记录。
尝试为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

推荐阅读