指出没有数据存在

指出没有数据存在

本文介绍了.NextResult() 方法确实给出了一个错误,指出没有数据存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能检查我的编码并让我知道我做错了什么吗?

Can you check my coding and let me know what I'm doing wrong?

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

I'm trying to use the .NextResult() method of a DataReader but I get an error that no data is present.

第一个查询返回一个值,但第二个查询是问题所在.

The 1st query returns a value but the 2nd query is the problem.

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()

reader.Read()

在 reader.NextResult 这个区域编码后:

after the reader.NextResult this area of coding:

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

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

I hope this helps someone who gets stuck like I did.

这篇关于.NextResult() 方法确实给出了一个错误,指出没有数据存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 10:41