繁体   English   中英

使用vb.net在一个按钮中更新和插入mysql记录

[英]Updating and Inserting mysql records in one button using vb.net

我正在尝试更新和插入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

UPDATE

我仍然无法解决问题,我通过包含一个文本框并让其读取ID来作弊,然后告诉它保存是否为空。 我希望正确执行此操作,以便有人能鸣叫,我将不胜感激。

自从我使用VB以来已经有很多年了,所以我确定语法会有些偏离。 任何人都可以根据需要进行编辑。

在您的按钮中单击事件处理程序

If LEN(ListView1.FocusedItem.SubItems(1).Text) > 0 Then
    updateContact()
Else
    saveCustomer()
End If

注意我在这里假设ID是在

ListView1.FocusedItem.SubItems(1).Text

并且,如果正在编辑的记录是新记录,则该结果将为空字符串。 如果这个假设是错误的,那么您将必须共享如何知道ID是否可用于插入/更新的信息。

UPDATE

如果我在尝试创建新记录之前未选择记录,则会崩溃

在这种情况下,而不是检查

ListView1.FocusedItem.SubItems(1).Text

您应该检查ListView1是否有任何重点项目。

If ListView1.FocusedItem <> NULL Then
    updateContact()
Else
    saveCustomer()
End If

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM