.NextResult() 方法确实给出了一个错误,指出不存在数据

Posted

技术标签:

【中文标题】.NextResult() 方法确实给出了一个错误,指出不存在数据【英文标题】:.NextResult() method does gives an error that states that no data is present 【发布时间】:2012-03-07 02:57:18 【问题描述】:

你能检查我的编码并告诉我我做错了什么吗?

我正在尝试使用 DataReader 的 .NextResult() 方法,但我收到一个错误,即不存在数据。

第一个查询返回一个值,但第二个查询是问题。

Dim strSqlStatement As String = "Select Count(*) As TotalRows " & _
                                          "From Parents " & _
                                         "Where (FatherName = @SearchValue " & _
                                         "   Or  MotherName = @SearchValue);"

strSqlStatement = strSqlStatement & "Select FatherName, MotherName " & _
                                          "From Parents " & _
                                         "Where (FatherName = @SearchValue " & _
                                         "   Or  MotherName = @SearchValue)"

' Set up the sql command and lookup the parent.
'----------------------------------------------
Using objSqlCommand As SqlCommand = New SqlCommand(strSqlStatement, ObjConnection)

    With objSqlCommand

        ' Add SqlParameters to the SqlCommand.
        '-------------------------------------
        .Parameters.Clear()
        .Parameters.AddWithValue("@SearchValue", TextBoxParentsName.Text)

        ' Open the SqlConnection before executing the query.
        '---------------------------------------------------
        Try
            ObjConnection.Open()

            ' Execute the query to see if the parents are in the database.
            '-------------------------------------------------------------

            ' Display the parent info.
            '-------------------------
            Dim reader As SqlDataReader = .ExecuteReader()

            reader.Read()

            Dim countOfRows = reader("TotalRows")

            If countOfRows = 1 Then

                reader.NextResult()

                TextBoxParentsName.Text = reader("FatherName").ToString()
                LabelBothParents.Text = "Father: " & TextBoxParentsName.Text & " Mother: " & reader("MotherName")
            End If

       Catch exErrors As Exception

            MessageBox.Show("Sorry, there was an error. Details follow: " & _
                                    vbCrLf & vbCrLf & exErrors.Message, _
                                    "Error")

            TextBoxParentsName.Focus()
        Finally
            blnDisableParentIdTextChanged = False

            ObjConnection.Close()
        End Try

    End With ' objSqlCommand
End Using ' objSqlCommand

【问题讨论】:

【参考方案1】:

找到缺失的语句:

我需要补充:

reader.Read()

reader.NextResult这个区域的编码之后:

If countOfRows = 1 Then

    reader.NextResult()

    reader.Read() ' This is what I needed to add.

    TextBoxParentsName.Text = reader("FatherName").ToString()
    LabelBothParents.Text = "Father: " & TextBoxParentsName.Text & " Mother: " & reader("MotherName")
End If

我希望这可以帮助像我一样陷入困境的人。

【讨论】:

以上是关于.NextResult() 方法确实给出了一个错误,指出不存在数据的主要内容,如果未能解决你的问题,请参考以下文章

DbDataReader、NextResult() 和填充多个表

使用 Dapper 的 NextResult

sql NextResult()多个结果集

Gitlab CI/CD 管道给出 Dockerfile 错误

在批处理文件中将SQL文件与命令`copy`结合使用会引入错误的语法,因为它确实添加了一个不可见的字符`U + FEFF`

Python AmazonAPI 错误 503 服务不可用