如何避免VB.NET中listview更新的重复记录

时间:2014-09-05 05:40:24

标签: mysql vb.net listview

我在插入记录时没有问题,如果它有重复,但没有更新记录,我真的不知道该怎么做。

这是我的代码:

Imports MySql.Data.MySqlClient
Public Class Form3
Public id As Integer
Public sConnection As New MySqlConnection
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If sConnection.State = ConnectionState.Closed Then
        sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
        sConnection.Open()
    End If
    LoadPeople()

End Sub
Private Sub btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnedit.Click
    If id = Nothing Then
        MsgBox("Please choose a record to edit.", MsgBoxStyle.Exclamation)
    Else
        Dim sqlQuery As String = "SELECT * FROM candidate WHERE cid = '" & lvPeople.SelectedItems(0).Text & "'"
        Dim sqlAdapter As New MySqlDataAdapter
        Dim sqlCommand As New MySqlCommand
        Dim sqlTabble As New DataTable

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = sConnection
            .ExecuteNonQuery()
        End With

        With sqlAdapter
            .SelectCommand = sqlCommand
            .Fill(sqlTabble)
        End With
        Form5.id = lvPeople.SelectedItems(0).Text
        Form5.cfname = sqlTabble.Rows(0)("cfname")
        Form5.cmname = sqlTabble.Rows(0)("cmname")
        Form5.clname = sqlTabble.Rows(0)("clname")
        Form5.cpos = sqlTabble.Rows(0)("cpos")
        Form5.cyr = sqlTabble.Rows(0)("cyr")
        Form5.cparty = sqlTabble.Rows(0)("cparty")
        Form5.vyear = sqlTabble.Rows(0)("vyear")
        Form5.ShowDialog()
        id = Nothing
    End If



End Sub

Private Sub lvPeople_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvPeople.MouseClick
    id = lvPeople.SelectedItems(0).Selected
End Sub

我的Listview代码:

Public Sub LoadPeople()
    Dim sqlQuery As String = "select * from candidate where cfname<>'Select a Candidate'"
    Dim sqlAdapter As New MySqlDataAdapter
    Dim sqlCommand As New MySqlCommand
    Dim TABLE As New DataTable
    Dim i As Integer

    With sqlCommand
        .CommandText = sqlQuery
        .Connection = sConnection
    End With

    With sqlAdapter
        .SelectCommand = sqlCommand
        .Fill(TABLE)
    End With

    lvPeople.Items.Clear()

    For i = 0 To TABLE.Rows.Count - 1
        With lvPeople
            .Items.Add(TABLE.Rows(i)("cid"))
            With .Items(.Items.Count - 1).SubItems
                .Add(AddFieldValue(TABLE.Rows(i), ("cpos")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cfname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cmname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("clname")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cyr")))
                .Add(AddFieldValue(TABLE.Rows(i), ("cparty")))
                .Add(AddFieldValue(TABLE.Rows(i), ("vyear")))
            End With
        End With
    Next

End Sub

Private Function AddFieldValue(ByVal row As DataRow, ByVal fieldName As String) As String
    If Not DBNull.Value.Equals(row.Item(fieldName)) Then
        Return CStr(row.Item(fieldName))
    Else
        Return Nothing
    End If
End Function

如果按下update按钮,则会弹出此edit表单

Imports MySql.Data.MySqlClient
Public Class Form5
Friend id As Integer
Friend cfname As String
Friend clname As String
Friend cmname As String
Friend cpos As String
Friend cyr As String
Friend cparty As String
Friend vyear As Integer

Public sConnection As New MySqlConnection

Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If sConnection.State = ConnectionState.Closed Then
        sConnection.ConnectionString = "server=localhost;user id=root;database=db;password=root"
        sConnection.Open()
    End If

    txtfname.Text = cfname
    txtlname.Text = clname
    txtmname.Text = cmname
    cmbpos.Text = cpos
    cmbyr.Text = cyr
    txtparty.Text = cparty
    TextBox1.Text = vyear

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim conn As New MySqlConnection
    Dim cmd As New MySqlCommand
    Dim dr As MySqlDataReader

    conn.ConnectionString = "server = localhost; user id = root; database = db; password = root"
    cmd.Connection = conn
    conn.Open()
    cmd.CommandText = " SELECT * FROM candidate WHERE cfname LIKE '" & txtfname.Text & "' AND cmname='" & txtmname.Text & "' AND clname='" & txtlname.Text & "'"
    dr = cmd.ExecuteReader

    If dr.HasRows Then
        MessageBox.Show("Entry already exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    ElseIf txtfname.Text = "" Or txtlname.Text = "" Or txtmname.Text = "" Or txtparty.Text = "" Or cmbyr.Text = "" Or cmbpos.Text = "" Then
        MessageBox.Show("Please complete the required fields..", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
    Else
        Dim sqlQuery As String = "UPDATE candidate SET cfname='" & txtfname.Text & "', cmname='" & txtmname.Text & "', clname='" & txtlname.Text & "' , cpos='" & cmbpos.Text & "', cyr='" & cmbyr.Text & "', cparty='" & txtparty.Text & "' WHERE cid='" & id & "'"
        Dim sqlCommand As New MySqlCommand

        With sqlCommand
            .CommandText = sqlQuery
            .Connection = sConnection
            .ExecuteNonQuery()
        End With
        MsgBox("Record Updated")
        Dispose()
    End If


    Form3.LoadPeople()
    Me.Close()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Me.Hide()
End Sub
End Class

当我按下edit按钮时,我试图删除记录,这样当我按update按钮时,它将找不到条目然后插入编辑的条目,并在我按下{时插入相同的条目{1}}按钮,是的,它不起作用。

0 个答案:

没有答案