NextResult()方法确实给出了一个错误,表示不存在任何数据



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

我正在尝试使用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

找到丢失的语句:

我需要添加:

reader.Read()

在读者之后。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

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

相关内容

最新更新