SqlDataReader HasRows但不读取

时间:2014-02-12 01:38:10

标签: asp.net vb.net sqldatasource

我正在使用SqlDataReader检索数据库信息以放入javascript字符串。我得到SqlDataReader.HasRows=True,如果我先调用我的字符串构建而不先读取数据读取器,我会得到正确的字符串。如果我尝试在阅读器上循环,没有任何结果。

以下代码返回正确形成的字符串,但仅针对第一条记录:

Function sFnLoadCodingLines() As String
    Dim cmdCoding As SqlCommand, drCoding As SqlDataReader, con As New SqlConnection(strcon)
    Dim sTemp As String = ""
    Dim lInvoiceID As Long

    If Not IsNumeric(CLng(Page.Request.QueryString("InvoiceID"))) Then
        sFnLoadCodingLines = ""
        Exit Function
    End If


    lInvoiceID = CLng(Page.Request.QueryString("InvoiceID"))

    cmdCoding = New SqlCommand
    cmdCoding.CommandText = "SELECT BU,DEPT,AccNo,Qty,Amount,Description,EMP,VARIETAL,BRAND,STATE,PROJECT " & _
                                "FROM OI_tblCoding WHERE isDeleted=0 AND InvoiceID=" & lInvoiceID
    cmdCoding.Connection = con
    con.Open()
    drCoding = cmdCoding.ExecuteReader
    drCoding.Read()

    If drCoding.HasRows() Then

        sTemp = sTemp & "addRowToTable(""" & drCoding("BU").ToString & _
            """,""" & drCoding("DEPT").ToString & _
            """,""" & drCoding("AccNo").ToString & _
            """,""" & drCoding("Qty").ToString & _
            """,""" & drCoding("Amount").ToString & _
            """,""" & drCoding("Description").ToString & _
            """,""" & drCoding("EMP").ToString & _
            """,""" & drCoding("VARIETAL").ToString & _
            """,""" & drCoding("BRAND").ToString & _
            """,""" & drCoding("STATE").ToString & _
            """,""" & drCoding("PROJECT").ToString & """);" & vbCrLf

        sFnLoadCodingLines = sTemp

    Else
        sFnLoadCodingLines = ""
    End If

    con.Close()

End Function

以下记录返回一个空字符串:

Function sFnLoadCodingLines() As String
    Dim cmdCoding As SqlCommand, drCoding As SqlDataReader, con As New SqlConnection(strcon)
    Dim sTemp As String = ""
    Dim lInvoiceID As Long

    If Not IsNumeric(CLng(Page.Request.QueryString("InvoiceID"))) Then
        sFnLoadCodingLines = ""
        Exit Function
    End If


    lInvoiceID = CLng(Page.Request.QueryString("InvoiceID"))

    cmdCoding = New SqlCommand
    cmdCoding.CommandText = "SELECT BU,DEPT,AccNo,Qty,Amount,Description,EMP,VARIETAL,BRAND,STATE,PROJECT " & _
                                "FROM OI_tblCoding WHERE isDeleted=0 AND InvoiceID=" & lInvoiceID
    cmdCoding.Connection = con
    con.Open()
    drCoding = cmdCoding.ExecuteReader
    drCoding.Read()

    If drCoding.HasRows() Then

        While drCoding.Read()
            'addRowToTable(sBU, sDPT, sAcct, dbQty, dbAmt, sDesc, sEmp, sVar, sBrand, sState, sProj)
            sTemp = sTemp & "addRowToTable(""" & drCoding("BU").ToString & _
                            """,""" & drCoding("DEPT").ToString & _
                            """,""" & drCoding("AccNo").ToString & _
                            """,""" & drCoding("Qty").ToString & _
                            """,""" & drCoding("Amount").ToString & _
                            """,""" & drCoding("Description").ToString & _
                            """,""" & drCoding("EMP").ToString & _
                            """,""" & drCoding("VARIETAL").ToString & _
                            """,""" & drCoding("BRAND").ToString & _
                            """,""" & drCoding("STATE").ToString & _
                            """,""" & drCoding("PROJECT").ToString & """);" & vbCrLf

        End While

        sFnLoadCodingLines = sTemp

    Else
        sFnLoadCodingLines = ""
    End If

    con.Close()

End Function

1 个答案:

答案 0 :(得分:3)

但是你正在阅读两种情况下的数据。在调用HasRows之前,调用ExecuteReader然后立即调用Read。摆脱第一次Read调用,你的循环将按预期工作。然后第一个代码将失败,因为你根本不会调用Read。