想要通过Visual Studio中的VB语言sql语句来实现数据库数据的修改,代码如下:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
constr = "Provider=Microsoft.ACE.OLEDB.12.0;
Data Source=D:\窗口文件\温度传感数据\温度传感数据库.accdb;Persist Security Info=False;"
Dim objconn As New OleDbConnection(constr) '创建连接对象
Dim selectedRows As DataGridViewSelectedRowCollection = DataGridView1.SelectedRows
Dim judge
For Each row As DataGridViewRow In selectedRows
' 获取选中行的数据
Dim rowData1 As String = row.Cells("ID").Value.ToString()
' 处理选中行的数据
Next
updatesql = "UPDATE 温度传感数据表 SET 温度='" & TextBox1.Text & "',日期='" & TextBox2.Text & "',时间='" & TextBox3.Text & "' WHERE ID=' " & DataGridView1.CurrentRow.Cells(0).Value & " ' "
Dim cmd As New OleDbCommand(updatesql, objconn) '定义OLEDB语句(sql语句,数据库连接)
objconn.Open() '打开连接
judge = cmd.ExecuteNonQuery() '执行sql语句
Try
judge = cmd.ExecuteNonQuery() '执行sql语句
Catch ex As Exception
MsgBox("修改失败!")
End Try
If judge <> 0 Then
MsgBox("修改成功!")
End If
End Sub
但是在updatesql的查询语句遇见了问题,不是很明白要怎么用where限制选择的范围来修改特定的数据。报的错是数据不匹配,如下图