使用vb6将记录集分配给sql数据库

时间:2009-11-27 17:14:57

标签: vb6

HI,

我在vb6中创建了记录集,并且存储了我从txt文件中读取的值。当iam尝试执行sql查询时,会将我的记录集数据插入到数据库表中。我收到的错误就像是 '''EOF或BOF为真,或者当前重新删除已被删除。请求的操作需要当前记录。 “”“

我只是附上我的代码,任何人都可以帮助我做错了。我认为在删除个人姓名时存在错误。但我不知道它是什么。

任何帮助将不胜感激。

Public Function ProcessDNE(ByVal strFileName As String) As Boolean

Dim intFileNbr As Integer
Dim strCurrentLine As String
Dim strRoutingNbr As String
Dim strAcct As String
Dim strIndividualName As String
Dim strAmount As String
Dim curAmount As Currency
Dim strParseString As String
Dim strParseFirstNm As String
Dim strParseMidInit As String
Dim strParseLastNam As String
Dim lngMidInitPos As Long
Dim lngParsePos1 As Long
Dim lngParsePos2 As Long
Dim lngParsePos3 As Long
Dim lngParsePos4 As Long
Dim lngParsePos5 As Long
Dim lngParsePos6 As Long
Dim lngPos As Long

    frmDNELoad.lblStatus.Caption = "Reading File..."
    frmDNELoad.Refresh

    '' # Set up rcdDNE structure
    With rcdDNE.Fields
        .Append "RTN", adVarChar, 9
        .Append "AccountNbr", adVarChar, 17
        .Append "IndividualName", adVarChar, 22
        .Append "FirstName", adVarChar, 50
        .Append "MiddleName", adVarChar, 1
        .Append "LastName", adVarChar, 50
        .Append "Amount", adCurrency
    End With

    rcdDNE.Open
    intFileNbr = FreeFile(1)
    Open strFileName For Input As #intFileNbr Len = 95 '' # Open file for input.
    Do While Not EOF(intFileNbr)
       Line Input #intFileNbr, strCurrentLine
       If Mid(strCurrentLine, 1, 1) = 6 Then
         strRoutingNbr = Mid(strCurrentLine, 4, 8)
         strAcct = Trim(Mid(strCurrentLine, 13, 17))
         strIndividualName = Trim(Mid(strCurrentLine, 55, 22))
         strAmount = Trim(Mid(strCurrentLine, 30, 10))
         strAmount = Left(strAmount, Len(strAmount) - 1)
         curAmount = CCur(strAmount)

       '' # Add new record to temporary recordset
            With rcdDNE
                .AddNew
                .Fields![RTN] = strRoutingNbr
                .Fields![AccountNbr] = strAcct
                .Fields![IndividualName] = strIndividualName
                .Fields![Amount] = curAmount
                .Update
            End With
       End If
    Loop
    Close #intFileNbr

    frmDNELoad.lblStatus.Caption = "Formatting Names..."
    frmDNELoad.Refresh
    DoEvents

    '' # Parse the IndividualName field
    rcdDNE.MoveFirst
    Do Until rcdDNE.EOF
        lngMidInitPos = 0
        lngParsePos1 = 0
        lngParsePos2 = 0
        lngParsePos3 = 0
        lngParsePos4 = 0
        lngParsePos5 = 0
        lngParsePos6 = 0
        strParseString = ""
        strParseFirstNm = ""
        strParseMidInit = ""
        strParseLastNam = ""

        strParseString = Trim(rcdDNE.Fields![IndividualName])
        '' # Replace double spaces ("  ") with a single space (" ")
        lngPos = InStr(1, strParseString, "  ")
        Do While lngPos
            strParseString = Mid(strParseString, 1, lngPos - 1) & Mid(strParseString, lngPos + 1, Len(strParseString))
            lngPos = InStr(1, strParseString, "  ")
        Loop

        '' # Locate positions of remaining spaces
        lngParsePos1 = InStr(1, strParseString, " ")
        If lngParsePos1 = 0 Then
            lngParsePos2 = 0
        Else
            lngParsePos2 = InStr(lngParsePos1 + 1, strParseString, " ")
        End If

        If lngParsePos2 = 0 Then
            lngParsePos3 = 0
        Else
            lngParsePos3 = InStr(lngParsePos2 + 1, strParseString, " ")
        End If
        If lngParsePos3 = 0 Then
            lngParsePos4 = 0
        Else
            lngParsePos4 = InStr(lngParsePos3 + 1, strParseString, " ")
        End If
        If lngParsePos4 = 0 Then
            lngParsePos5 = 0
        Else
            lngParsePos5 = InStr(lngParsePos4 + 1, strParseString, " ")
        End If
        If lngParsePos5 = 0 Then
            lngParsePos6 = 0
        Else
            lngParsePos6 = InStr(lngParsePos5 + 1, strParseString, " ")
        End If

        '' # Determine if Middle initial is present
        If (lngParsePos3 - lngParsePos2) = 2 Then
            lngMidInitPos = lngParsePos2 + 1
            rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
        ElseIf (lngParsePos4 - lngParsePos3) = 2 Then
            lngMidInitPos = lngParsePos3 + 1
            rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
        ElseIf (lngParsePos5 - lngParsePos4) = 2 Then
            lngMidInitPos = lngParsePos4 + 1
            rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
        ElseIf (lngParsePos6 - lngParsePos5) = 2 Then
            lngMidInitPos = lngParsePos5 + 1
            rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
        ElseIf (lngParsePos2 - lngParsePos1) = 2 Then
            lngMidInitPos = lngParsePos1 + 1
            rcdDNE.Fields![MiddleName] = Mid(strParseString, lngMidInitPos, 1)
        End If

        '' # If there is a middle initial, everything to the left of it goes into the
        '' # first name field, and everything to the right of it goes into the last
        '' # name field. If there is no middle initial, everything after the first space
        '' # goes into the last name field.
        If lngMidInitPos <> 0 Then
            rcdDNE.Fields![FirstName] = Trim(Left(strParseString, lngMidInitPos - 1))
            rcdDNE.Fields![LastName] = Trim(Mid(strParseString, lngMidInitPos + 1, Len(strParseString)))
        Else
            rcdDNE.Fields![FirstName] = Trim(Left(strParseString, lngParsePos1))
            rcdDNE.Fields![LastName] = Trim(Mid(strParseString, lngParsePos1 + 1, Len(strParseString)))
        End If
        rcdDNE.Update
        rcdDNE.MoveNext
    Loop

    '' # Write records to Database

    frmDNELoad.lblStatus.Caption = "Loading data into database......"
    Call FindServerConnection_NoMsg

'' #    Do Until rcdDNE.EOF
'' #   rcdDNE.MoveFirst
'' #    cmdCommand.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") & "')"
'' #    cmdCommand.Execute ()
'' #    rcdDNE.MoveNext
'' #    Loop

    Dim lngRecCount As Long
    lngRecCount = 0

    Set rcdReclamation = New ADODB.Recordset
    With rcdReclamation
        .ActiveConnection = objConn
        .Source = "insert into t_DATA_DneFrc (RTN, AccountNbr, FirstName, MiddleName, LastName, Amount) values ('" & rcdDNE("RTN") & "', '" & rcdDNE("AccountNbr") & "', '" & rcdDNE("FirstName") & "', '" & rcdDNE("MiddleName") & "', '" & rcdDNE("LastName") & "', '" & rcdDNE("Amount") & "')"
        .CursorType = adOpenDynamic
        .CursorLocation = adUseClient
        .LockType = adLockOptimistic
        .Open , , , , adCmdStoredProc
    End With

    rcdDNE.MoveFirst
    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

1 个答案:

答案 0 :(得分:0)

我的猜测是错误发生在'rcdReclamation.Source = ...'上,因为您在构建查询时正在访问rcdDNE记录集中的项目。在您设置rcdReclamation之前,您经历了一个rcdDNE循环,直到它是EOF。因此,当您创建查询时,rcdDNE保证返回EOF。

编辑:可能的解决方案 这是因为我以这种方式使用记录集(我通常只使用查询并在连接上执行它们)所以我可能会稍微偏离一点。要将所有记录实际保存到数据库,您需要调用UpdateBatch。使用AddNew创建记录和更新以保存更改时,需要在所有添加/编辑结束时调用UpdateBatch以将更改提交到数据库。所以我会用一个简单的rcdDNE.UpdateBatch替换从“将数据加载到数据库......”行到最后的所有内容。