vb.net - 单元格未更新 - 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
表中的列 Where 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
推荐阅读
- android - 在 Android Studio 中从账单图像中提取文本
- excel - 如何在不到两周的时间内完成条件格式化?
- android - 可以在 google play 中以不同的名称重新发布 apk 吗?
- azure - 如何将 TXT 记录复制到 Azure 门户中的 DNS 配置中?
- aws-sam-cli - 如何将 aws sam cli 与卷一起使用 --docker-volume-basedir
- javascript - addEventListener 在同一输入上多次执行函数
- keras - 在顺序 KERAS U-NET 中连接
- reactjs - 如何在 React Native 中使用选项卡制作视差滚动视图?
- spring - spring boot 框架省略错误不是依赖项
- sql - 系统用户/角色的密码是否以加密形式存储在 YugabyteDB YSQL 和 YCQL 中?