OleDbDataAdapter填充创建重复行

时间:2013-03-07 21:35:39

标签: database vb.net ms-access oledb

我遇到了一个问题,我使用数据适配器更新然后重新填充数据表。调用fill方法后,该行将被复制。一个ID具有正确的(新)ID,另一个ID显示为-1。下面的代码是完美的,是我想要更复杂的代码的简单形式。 Cosider以下:

Imports WindowsApplication1.testDataSet
Imports WindowsApplication1.testDataSetTableAdapters
Imports System.Data.OleDb
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        Dim DA As New testTableAdapter
        Dim DT As New testDataTable
        DA.Fill(DT)
        Dim NR As testRow = DT.Rows.Add
        NR.SomeText = "Test"
        Dim DA2 As New OleDbDataAdapter("SELECT * FROM test", _
        DA.Connection.ConnectionString)
        Dim CB As New OleDbCommandBuilder(DA2)
        DA2.Update(DT)
        DA.Fill(DT)
        For Each R As testRow In DT.Rows
            Debug.Print(R.ID)
        Next
    End Sub
End Class

上面的代码非常完美。它们的键列不显示-1,没有重复。现在考虑我的应用程序中的下面的代码,该代码会导致重复行与关键列在最后LoadLoadNumbers()之后生成-1。

    Dim AccountLoans As IEnumerable(Of LoanNumbersRow) = _
    From L As LoanNumbersRow In LoanNumbers _
    Select L Where L.AccountID = ID

    If Not frmFindLoans.IsDisposed AndAlso _
    frmFindLoans.DialogResult = Windows.Forms.DialogResult.OK Then
        For Each L As LoanNumbersRow In AccountLoans
            If (From R As DataGridViewRow In frmFindLoans.dgvLoans.Rows _
            Select R Where R.Cells("LoanNumber").Value = L.LoanNumber).Count = 0 Then
                If L.IsWhenDeletedNull Then
                    L.WhenDeleted = Now
                    L.DeletedBy = UserName()
                End If
            End If
        Next

        Dim NewLoan As LoanNumbersRow
        Dim FindLoan As IEnumerable(Of LoanNumbersRow)
        For Each R As DataGridViewRow In frmFindLoans.dgvLoans.Rows
            FindLoan = From L As LoanNumbersRow In LoanNumbers.Rows _
            Select L Where L.LoanNumber = R.Cells("LoanNumber").Value And _
            L.AccountID = ID
            If FindLoan.Count = 0 Then
                NewLoan = LoanNumbers.Rows.Add
                NewLoan.AccountID = Acc.AccountID
                NewLoan.LoanNumber = R.Cells("LoanNumber").Value
                NewLoan.LoanBusinessName = R.Cells("LoanBusiness").Value
                NewLoan.LoanBorrower = R.Cells("LoanBorrower").Value
                NewLoan.AddedBy = UserName()
                NewLoan.WhenAdded = Now
            End If
        Next
        Try
            Dim CB As New OleDbCommandBuilder(LoanNumbersAdapter)
            LoanNumbersAdapter.Update(LoanNumbers)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, "Error saving loan number data")
            Exit Sub
        End Try

        If Not LoadLoanNumbers() Then Exit Sub
    End If

来自模块的其他变量:

Public LoanNumbersAdapter As OleDbDataAdapter
Public LoanNumbers As New LoanNumbersDataTable
Public Sub InitializeAdapters()
    LoanNumbersAdapter = New OleDbDataAdapter( _
    "SELECT * FROM LoanNumbers WHERE WhenDeleted IS NULL ORDER BY WhenAdded DESC", AccountingConn)
End Sub
Public Function LoadData(ByVal DA As OleDbDataAdapter, ByVal DT As DataTable) As Boolean
    Try
        DA.Fill(DT)
        Return True
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Error loading the " & DT.TableName & " table")
        Return False
    End Try
End Function
Public Function LoadLoanNumbers() As Boolean
    Return LoadData(LoanNumbersAdapter, LoanNumbers)
End Function

为什么顶部的简单测试工作正常,但是我的实际应用程序在键列上创建了带有-1的重复行?我想我可以在更新后填充之前清除数据表,但是一旦它开始变成一个大表,就不会陷入困境吗? * BTW:数据库是MS访问,它是.NET 3.5

1 个答案:

答案 0 :(得分:0)

这是我的胶带解决方案:(

''' <summary>
''' Removes any rows where the ID/key column is less than zero
''' </summary>
<Extension()> Public Sub DeleteRelics(ByVal DT As DataTable)
    If DT.PrimaryKey.Count = 0 Then Exit Sub
    For Each R As DataRow In _
    (From Rows As DataRow In DT.Rows _
     Select Rows Where Rows(DT.PrimaryKey.First.ColumnName) < 0)
        R.Delete()
    Next
    DT.AcceptChanges()
End Sub