我正在尝试在用户修改数据库记录时更新它。我有一个名为“txtColsTextBox”的文本框,用于存储值和一个名为“btnSaveExit”的按钮。单击按钮时,我需要使用新值更新数据库。
我将如何根据我的代码执行此操作。我在想,我需要使用 me.validate 函数,但不知道如何编码。谢谢
Dim connetionString As String
Dim oledbCnn As OleDbConnection
Dim oledbCmd As OleDbCommand
Dim sql As String
connetionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\domain\storage.accdb"
sql = "SELECT Cols FROM Racks Where [Rack code] = '" & buttonName & "'"
oledbCnn = New OleDbConnection(connetionString)
Try
oledbCnn.Open()
oledbCmd = New OleDbCommand(sql, oledbCnn)
Dim oledbReader As OleDbDataReader = oledbCmd.ExecuteReader()
While oledbReader.Read
'MsgBox(oledbReader.Item(0))
txtColsTextBox.Text = oledbReader.Item(0)
End While
oledbReader.Close()
oledbCmd.Dispose()
oledbCnn.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
编辑:更新数据库的代码
Private Sub btnSaveExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveExit.Click
Try
connetionString = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\domain\storage.accdb"
oledbCnn.Open()
oledbCmd = New OleDbCommand(sql, oledbCnn)
oledbCmd.CommandText = "UPDATE [Racks] SET [Cols] VALUES (?)"
oledbCmd.Parameters.AddWithValue("@Cols", txtColsTextBox.Text)
oledbCmd.ExecuteNonQuery()
MessageBox.Show("Record succesfully updated" + txtColsTextBox.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub