我正在尝试在 MySQL 数据库中更新和插入记录,我可以在单独的按钮中执行这两项操作,但我想通过一个按钮执行这两个查询。 我想做类似的事情: 如果 ID(主键)= Something then 更新,否则插入,但我无法弄清楚我可以将其作为基础。我将在下面包含我的一些代码。
正在插入
Private Function saveCustomer()
Dim command As MySqlCommand = Nothing
Dim query As String = "INSERT INTO contacts (first_name, surname, house_number, street, suburb, state, phone, mobile, work, email, notes) VALUES (@first_name, @surname, @housenumber, @street, @suburb, @state, @phone, @mobile, @work, @email, @notes)"
Try
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
command = New MySqlCommand(query, connection)
command.Parameters.AddWithValue("@first_name", txtFirstName.Text)
command.Parameters.AddWithValue("@surname", txtSurname.Text)
command.Parameters.AddWithValue("@housenumber", txtHouseNo.Text)
command.Parameters.AddWithValue("@street", txtStreet.Text)
command.Parameters.AddWithValue("@suburb", txtSuburb.Text)
command.Parameters.AddWithValue("@state", cboState.Text)
command.Parameters.AddWithValue("@phone", txtPhone.Text)
command.Parameters.AddWithValue("@mobile", txtMobile.Text)
command.Parameters.AddWithValue("@work", txtWork.Text)
command.Parameters.AddWithValue("@email", txtEmail.Text)
command.Parameters.AddWithValue("@notes", txtNotes.Text)
command.ExecuteNonQuery()
MessageBox.Show("Contact Saved Sucessfully")
Return True
Catch ex As MySqlException
Return False
Finally
connection.Close()
command.Dispose()
End Try
End Function
更新中
Private Sub updateContact()
Dim command As MySqlCommand = Nothing
Dim query As String = "UPDATE contacts SET first_name=@first_name, surname=@surname, house_number=@housenumber, street=@street, suburb=@suburb, state=@state, phone=@phone, mobile=@mobile, work=@work, email=@email, notes=@notes WHERE id= '" & ListView1.FocusedItem.SubItems(1).Text & "'"
Try
connection.Open()
command = New MySqlCommand(query, connection)
command.Parameters.AddWithValue("@first_name", txtFirstName.Text)
command.Parameters.AddWithValue("@surname", txtSurname.Text)
command.Parameters.AddWithValue("@housenumber", txtHouseNo.Text)
command.Parameters.AddWithValue("@street", txtStreet.Text)
command.Parameters.AddWithValue("@suburb", txtSuburb.Text)
command.Parameters.AddWithValue("@state", cboState.Text)
command.Parameters.AddWithValue("@phone", txtPhone.Text)
command.Parameters.AddWithValue("@mobile", txtMobile.Text)
command.Parameters.AddWithValue("@work", txtWork.Text)
command.Parameters.AddWithValue("@email", txtEmail.Text)
command.Parameters.AddWithValue("@notes", txtNotes.Text)
command.ExecuteNonQuery()
MessageBox.Show("Contact Updated Successfully")
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
connection.Close()
command.Dispose()
End Try
End Sub
这会从数据库中填充我的 ListView
Private Sub loadcontacts()
Dim command As MySqlCommand = Nothing
Dim listquery As String = "SELECT * FROM contacts ORDER BY id"
Dim reader As MySqlDataReader = Nothing
Try
If connection.State = ConnectionState.Closed Then
connection.Open()
End If
command = New MySqlCommand(listquery, connection)
reader = command.ExecuteReader()
command.CommandText = listquery
With ListView1
.Columns.Add("Name", 220, HorizontalAlignment.Left)
End With
While reader.Read
Dim ls As New ListViewItem(reader.Item("first_name").ToString() & " " & reader.Item("surname").ToString)
ls.SubItems.Add(reader.Item("id").ToString)
ListView1.Items.Add(ls)
End While
Catch ex As MySqlException
Finally
connection.Close()
command.Dispose()
End Try
End Sub
更新
我仍然无法解决,我通过包含一个文本框并让它读取 ID 来作弊,然后我告诉它如果它是空的就保存。我宁愿正确地做到这一点,所以如果有人能插话,我将不胜感激。
最佳答案
自从我使用 VB 以来已经有很长时间了,所以我确信语法有点不对劲。任何人都可以根据需要随意编辑。
在您的按钮点击事件处理程序中
If LEN(ListView1.FocusedItem.SubItems(1).Text) > 0 Then
updateContact()
Else
saveCustomer()
End If
注意我这里假设ID设置在
ListView1.FocusedItem.SubItems(1).Text
而且,如果正在编辑的记录是新的,这将评估为一个空字符串。如果该假设是错误的,您将必须分享您如何知道 ID 是否可用于插入/更新。
更新
it crashes if I don't select a record before I try to create a new record
在那种情况下,而不是检查
ListView1.FocusedItem.SubItems(1).Text
您应该检查 ListView1 是否有任何焦点项目。
If ListView1.FocusedItem <> NULL Then
updateContact()
Else
saveCustomer()
End If
关于mysql - 使用vb.net一键更新和插入mysql记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28732163/