根据列表框项检查现有数据库记录项

时间:2016-07-20 17:50:49

标签: vb.net listbox ado.net

我有一个数据表,我在windows forms application添加记录。此数据表只有2列,第一列是主键,是一个整数。第二列包含名称,我需要自动将列表框中的名称列表添加到表中。这些名称中的大多数已经在表格中有自己的记录,但每次列表框中都会有不同的名称。我需要检查列表框项目的现有记录项,以确保没有添加重复项,但如果列表框中有一个名称不存在于数据表中,请为该名称添加新记录。

到目前为止我所尝试的是:

Private m_cn As New SqlConnection()
Private m_DA As SqlDataAdapter
Private m_CB As SqlCommandBuilder
Private m_DataTable As New DataTable
Private m_IntRowPosition As Integer = 0

Private Sub btnInsertIntoDatabase_Click(sender As Object, e As EventArgs) Handles btnInsertIntoDatabase.Click
    Dim drReadRow As DataRow = m_DataTable.NewRow()
    Dim dcReadCol As DataColumn = m_DataTable.Columns.Item(1)
    Dim intLoopCounter As Integer = 0
    Dim unique As Boolean = True

    If m_DataTable.Rows.Count = 0 Then
        For m_IntRowPosition = 0 To (lstScannedNames.Items.Count() - 1)
            Dim drNewRow As DataRow = m_DataTable.NewRow()
            drNewRow("emp_id") = m_IntRowPosition
            drNewRow("emp_name") = RTrim(lstScannedNames.Items.Item(RTrim(m_IntRowPosition)))

            m_DataTable.Rows.Add(drNewRow)
        Next m_IntRowPosition
        GoTo SomeWhereElse
        ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ElseIf m_DataTable.Rows.Count > 0 Then

        For m_IntRowPosition = 0 To m_DataTable.Rows.Count
            For intLoopCounter = 0 To lstScannedNames.Items.Count - 1
                If RTrim(m_DataTable.Rows(m_IntRowPosition).Item(1)) = lstScannedNames.Items.Item(intLoopCounter) Then
                    unique = False

                ElseIf RTrim(m_DataTable.Rows(m_IntRowPosition).Item(1)) <> lstScannedNames.Items.Item(intLoopCounter) Then
                    unique = True
                    lstScannedNames.SelectedIndex = intLoopCounter
                    intLoopCounter = lstScannedNames.Items.Count - 1
                End If
            Next intLoopCounter
            If (unique) = True Then
                Dim drNewRow As DataRow = m_DataTable.NewRow()
                drNewRow("emp_id") = m_DataTable.Rows.Count()
                drNewRow("emp_name") = lstScannedNames.Items.Item(lstScannedNames.SelectedIndex)
                m_DataTable.Rows.Add(drNewRow)
            Else
                unique = True
            End If
        Next m_IntRowPosition
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    End If
SomeWhereElse:
    m_DA.Update(m_DataTable)
    MessageBox.Show("Operation Completed")
End Sub

焦点领域是ElseIf声明。当我运行此代码并且数据库中已有记录时,它会将列表中的第一个名称添加到数据表中,而不是添加到数据表中。然后,它会将列表中的第一个名称添加到新记录中,对于列表中的每个项目,大约是20次,但这取决于列表长度。我一直在尝试不同的事情,我知道我很接近,但我已经被困了一段时间。

1 个答案:

答案 0 :(得分:0)

扭转你的逻辑。遍历列表框中的项目并检查它们是否存在于数据表中

....
For intLoopCounter = 0 To lstScannedNames.Items.Count - 1
   Dim name = lstScannedNames.Items.Item(intLoopCounter)
   Dim rowExist = m_DataTable.Select("emp_name = '" & name & "'")
   if rowExist Is Nothing OrElse rowExist.Length = 0 Then
       Dim drNewRow As DataRow = m_DataTable.NewRow()
       drNewRow("emp_id") = m_DataTable.Rows.Count()
       drNewRow("emp_name") = name
       m_DataTable.Rows.Add(drNewRow)
   End If
Next m_IntRowPosition
...

使用DataTable.Select方法可以避免编写显式循环来查找重复项。当您确定没有重复

时,您可以直接添加该行