我试图在MySQL数据库中更新和插入记录,我可以在单独的按钮中执行这两项操作,但我想从一个按钮执行两个查询。 我想做的事情如下: 如果ID(主键)=那么 更新,否则插入,但我无法弄清楚我可以做什么。我会在下面提供一些代码。
INSERTING
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
UPDATING
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
而这是我从数据库中获取的信息
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来作弊,然后我告诉它保存,如果它是空的。我更愿意这样做,所以如果有人能说话,我会很感激。
答案 0 :(得分:1)
自从我使用VB以来已经很久了,所以我确信语法有点过时了。任何人都可以根据需要自由编辑。
在按钮单击事件处理程序
中If LEN(ListView1.FocusedItem.SubItems(1).Text) > 0 Then
updateContact()
Else
saveCustomer()
End If
注意我假设ID在
中设置ListView1.FocusedItem.SubItems(1).Text
并且,如果正在编辑的记录是新的,则将评估为空字符串。如果这个假设是错误的,那么您必须分享如何知道ID是否可用于插入/更新。
<强>更新强>
如果我在尝试创建新记录之前没有选择记录,那么它会崩溃
在这种情况下,而不是检查
ListView1.FocusedItem.SubItems(1).Text
你应该检查ListView1是否有任何重点项目。
If ListView1.FocusedItem <> NULL Then
updateContact()
Else
saveCustomer()
End If