我的表单上有一个DataGridView,如果我单击一行,则行详细信息将放置在表单上的相应控件中。然后,我可以对这些控件进行一些更改,然后单击“更新”按钮。令人惊讶的是,该代码未对数据库进行任何更改。我在想,这段代码有什么问题吗?这是我的应用程序的“更新”按钮代码。
Private Sub UpdateButton_Click(sender As Object, e As EventArgs) Handles updateButton.Click
Try
Using conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Tukabakoma.mdb;")
conn.Open()
Dim cmd As New OleDbCommand("Update Members set [Name]=@name,Middlename=@midname,Surname=@sname,ContactNumber=@contnum,Address=@address,DOB=@dob,Gender=@gender,[Status]=@status,JoinDate=@jd,POB=@pob,[Guardian Name]=@guardname,[Guardian Surname]=@guardsname,Relationship=@rel,GuardNumber=@gcontnum where TKBS_ID=@tkbsid", conn)
With cmd.Parameters
.AddWithValue("@tkbsid", tkbsIDTextBox.Text)
.AddWithValue("@name", nameTextBox.Text)
.AddWithValue("@midname", middleNameTextBox.Text)
.AddWithValue("@sname", surnameTextBox.Text)
.AddWithValue("@contnum", contactTextBox.Text)
.AddWithValue("@address", addressTextBox.Text)
.AddWithValue("@jd", jdDateTimePicker.Value)
If maleRadioButton.Checked = True Then
.AddWithValue("@gender", maleRadioButton.Text)
ElseIf femaleRadioButton.Checked = True Then
.AddWithValue("@gender", femaleRadioButton.Text)
End If
.AddWithValue("@status", statusComboBox.Text)
.AddWithValue("@dob", dobDateTimePicker.Value)
.AddWithValue("@pob", burialPlaceTextBox.Text)
.AddWithValue("@guardname", gNameTextBox.Text)
.AddWithValue("@guardsname", gSurnameTextBox.Text)
.AddWithValue("@rel", relationshipTextBox.Text)
.AddWithValue("@gcontnum", gNumberTextBox.Text)
End With
cmd.ExecuteNonQuery()
RefreshDataGridView()
MessageBox.Show("Member information successfuly updated!", "INFO", MessageBoxButtons.OK, MessageBoxIcon.Information)
cmd.Dispose()
conn.Close()
End Using
Catch ex As Exception
MessageBox.Show(ex.Message, "ERROR12", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
答案 0 :(得分:0)
这里,OleDb和Access的主要思想是将参数添加到Parameters集合中的顺序与它们在Sql语句中出现的顺序相同。
.AddWithValue("@rel", relationshipTextBox.Text)
和.AddWithValue("@gcontnum", gNumberTextBox.Text)
不在您的更新语句中,因此无法将它们添加到参数集合中。如果您更改Update语句以使其包含这些内容,则在将它们添加到Parameters集合时,请确保将它们放置在正确的位置。
由于End Using
行将关闭并释放它,因此无需关闭连接。如果您对OleDbCommand
也这样做,那会更好。也就是说,将其包含在Using块中。
Dim cmd As New OleDbCommand(Dim cmd As New OleDbCommand("Update Members set
[Name]=@name,
Middlename=@midname,
Surname=@sname,
ContactNumber=@contnum,
Address=@address,
DOB=@dob,
Gender=@gender,
[Status]=@status,
JoinDate=@jd,
POB=@pob,
[Guardian Name]=@guardname,
[Guardian Surname]=@guardsname,
Relationship=@rel,
GuardNumber=@gcontnum
where TKBS_ID=@tkbsid", conn)
With cmd.Parameters
.AddWithValue("@name", nameTextBox.Text)
.AddWithValue("@midname", middleNameTextBox.Text)
.AddWithValue("@sname", surnameTextBox.Text)
.AddWithValue("@contnum", contactTextBox.Text)
.AddWithValue("@address", addressTextBox.Text)
.AddWithValue("@dob", dobDateTimePicker.Value)
If maleRadioButton.Checked = True Then
.AddWithValue("@gender", maleRadioButton.Text)
ElseIf femaleRadioButton.Checked = True Then
.AddWithValue("@gender", femaleRadioButton.Text)
End If
.AddWithValue("@status", statusComboBox.Text)
.AddWithValue("@jd", jdDateTimePicker.Value)
.AddWithValue("@pob", burialPlaceTextBox.Text)
.AddWithValue("@guardname", gNameTextBox.Text)
.AddWithValue("@guardsname", gSurnameTextBox.Text)
.AddWithValue("@tkbsid", tkbsIDTextBox.Text)
End With
我必须提到.AddWithValue
并不是做到这一点的最佳方法。参见http://www.dbdelta.com/addwithvalue-is-evil/
和
https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/
还有一个:
https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications
首选方法是.Add(parameterName As String, OleDbType As OleDBType, Size As Integer).Value = yourValue