0

我试图找到包含特定参考号的每条记录,然后为每条记录将其分配给一个会话,以便可以将其添加到 PDF 中。当我运行下面的代码时,它只执行 1 个参考号,即使可能有 3 条记录我需要从中获取数据。知道为什么它不为每条记录执行它吗?TIA

      cmd = New SqlCommand
            cmd.Connection = Con
            cmd.CommandText = "SELECT [Physician First Name], [Physician Last Name], [Recipient Primary Business Street Address Line 1], [Recipient City], [Recipient State], [Recipient Zip Code] FROM tblData WHERE ReferenceNumber = @ReferenceNumber"
            cmd.Parameters.Add(New SqlParameter("@ReferenceNumber", (ReferenceTextBox.Text)))

    Dim reader As SqlDataReader = cmd.ExecuteReader()

    For Each NPIlookup In reader

                If (reader.Read()) Then

                    Session("DoctorFirstName") = reader(0)


                End If
                Session("PDF") &= Session("DoctorFirstName")

            Next
4

2 回答 2

5

The reader.Read() in your for each loop advances the reader to the next record, skipping the one you're interested in.

You shouldn't modify the collection (in this case, the reader) during the for each loop.

于 2013-09-24T14:05:39.217 回答
1

这不是 DataReader 的工作方式

枚举读取器时,您枚举的是当前记录的字段集合,而不是所有记录。您需要调用 reader.Read() 来获取下一条记录,然后获取具有Item 属性的 specifig 字段,使用例如获取转换为特定类型的值。GetReader,或使用 For Each 遍历阅读器的字段。

在此处查看读者的文档以获取示例。

要读取特定字段,您需要编写如下内容:

While reader.Read()
    Dim firstName=reader.GetString(0)
    ...
End While

遍历字段

While reader.Read()
    For Each value as Object in reader
        ...
    Next        
End While
于 2013-09-24T14:08:54.010 回答