mysql - 使用vb.net一键更新和插入mysql记录

标签 mysql vb.net

我正在尝试在 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/

相关文章:

mysql - 一对多连接三张表

php - 获取重复条目错误消息并在 if 语句中使用

php - Unescape html 以在 View 中正确显示

vb.net - 标签未出现在动态创建的表单 vb.net 上

mysql inner join 给出不好的结果(?)

C# 等效于 VB DLL 函数声明 (InternetSetOption)?

php - 可能使用 Levenshtein 距离匹配搜索词的准确性

php - Mysql数据库经常被锁

.net - 如何在 VB.NET 中引发事件之前检查订阅者

asp.net - 循环通过repeater控件来获取asp.net中Textbox的值