找到副本并将其写入日志文件中

时间:2009-11-30 21:23:01

标签: sql-server sql-server-2005 vb6

我有一个代码,它从文本文件中读取acc no,rtn,name和amt,并存储在记录集中。之后我创建了将记录集数据存储到sql server 2005表中的sql。

问题在于accno列是主键。但我的文本文件中有一些重复的accno。在将记录集添加到数据库时,如果它找到重复的accno,它会停在那里并且不会在该重复列之后插入任何行。

现在我想要做的是,如果有任何重复的列,我想将该列存储到日志文件中并跳过该列并将剩余的列插入数据库。我不知道该怎么做。有谁能够帮我。比如如何检查重复的列并跳过它并插入剩余的列。

    ' Write records to Database

    frmDNELoad.lblStatus.Caption = "Loading data into database......"
    Dim lngRecCount As Long
    lngRecCount = 0
    rcdDNE.MoveFirst

     With cmdCommand
        .ActiveConnection = objConn
        .CommandText = "insert into t_DATA_DneFrc (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount) values ('" & rcdDNE("RTN") & "', '" & rcdDNE("AccountNbr") & "', '" & rcdDNE("FirstName") & "', '" & rcdDNE("MiddleName") & "', '" & rcdDNE("LastName") & "', '" & rcdDNE("Amount") & "')"
        .CommandType = adCmdText
    End With

    Set rcddnefrc = New ADODB.Recordset
    With rcddnefrc
        .ActiveConnection = objConn
        .Source = "SELECT * FROM T_DATA_DNEFRC"
        .CursorType = adOpenDynamic
        .CursorLocation = adUseClient
        .LockType = adLockOptimistic
        .Open
    End With

    Do Until rcdDNE.EOF
        lngRecCount = lngRecCount + 1
        frmDNELoad.lblStatus.Caption = "Adding record " & lngRecCount & " of " & rcdDNE.RecordCount & " to database."
        frmDNELoad.Refresh
        DoEvents
        Call CommitNew
        rcdDNE.MoveNext
    Loop
    frmDNELoad.lblStatus.Caption = "DNE Processing Complete."
    frmDNELoad.Refresh        
End Function

    Sub CommitNew()

'     Add records to DneFrc table
    With rcddnefrc
        .Requery
        .AddNew
        .Fields![RTN] = rcdDNE.Fields![RTN]
        .Fields![AccountNbr] = rcdDNE.Fields![AccountNbr]
        .Fields![FirstName] = rcdDNE.Fields![FirstName]
        .Fields![MiddleName] = rcdDNE.Fields![MiddleName]
        .Fields![LastName] = rcdDNE.Fields![LastName]
        .Fields![Amount] = rcdDNE.Fields![Amount]
        .Update
    End With

End Sub

3 个答案:

答案 0 :(得分:1)

更多的策略然后是一个特定的答案,但......

从外部源导入数据时,我们经常会将数据插入到没有放置相同键/约束的临时表中,然后在插入之前清理数据。

在“卫生”期间所做的工作取决于您的要求(例如,当您有两个相同的帐号时,记录相同或数据字段不同,如果字段不同,您如何选择要使用的数据?)。然后在清理完成后将其插入/移动到生产表中。

答案 1 :(得分:1)

我遇到了这个问题,我所做的是创建一个集合,我将对象和密钥存储到密钥中。如果我尝试添加重复的密钥,我会收到错误。

这是我在vb6中发现的最简单的方法。在c#中是字典。

答案 2 :(得分:0)

我的建议是将错误处理添加到CommitNew以查看插入的行是否会创建主键违规,如果是,则执行其他处理。

示例:

Sub CommitNew()
  ''#Add records to DneFrc table 
  On Error GoTo CommitNew_Error
  With rcddnefrc
    .Requery
    .AddNew
    .Fields![RTN] = rcdDNE.Fields![RTN]
    .Fields![AccountNbr] = rcdDNE.Fields![AccountNbr]
    .Fields![FirstName] = rcdDNE.Fields![FirstName]
    .Fields![MiddleName] = rcdDNE.Fields![MiddleName]
    .Fields![LastName] = rcdDNE.Fields![LastName]
    .Fields![Amount] = rcdDNE.Fields![Amount]
    .Update
  End With
  Exit Sub   ''# If no error, exit routine.
CommitNew_Error:
  If Err.Number = -2147217873 Then
    ''# code here will only execute if the constraint violation occurs
    Call WriteDuplicateAccountToFile()
    Err.Clear()       ''# This clears the error, since you handled it
  Else
    ''# Do stuff with other errors.
    ''# If you're not sure, at least display what error its giving, like so
    MsgBox "The following error was encountered when new record was saved:" & _
      vbNewLine & CStr(Err.Number) & " - " & Err.Description & vbNewLine & _
      "New record not saved.", vbOkOnly + vbCritical, "Error"
  End If
End Sub