1

我正在尝试在 while 语句中为每个语句执行一个语句,但它没有在每个语句中执行我的代码。我假设没有以某种方式分配“价值”?

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], Notes_ConsultingFee, [Total Amount of Payment]  FROM tblData WHERE ReferenceNumber = @ReferenceNumber"
cmd.Parameters.Add(New SqlParameter("@ReferenceNumber", (ReferenceTextBox.Text)))
Dim reader As SqlDataReader = cmd.ExecuteReader()

While reader.Read()
    For Each value As Object In reader

        Dim firstName = reader.GetString(0)
        Dim LastName = reader.GetString(1)
        Dim Address = reader.GetString(2)
        Dim City = reader.GetString(3)
        Dim State = reader.GetString(4)
        Dim Zip = reader.GetString(5)
        Dim Notes = reader.GetString(6)
        Dim Amount As Decimal = reader(7)
        Session("PDF") &= firstName & " " & LastName & Environment.NewLine & Address & Environment.NewLine & City & "," & State & " " & Zip & Environment.NewLine & "Consulting Fee Amount: " & Amount & Environment.NewLine & "Notes: " & Notes
    Next
End While
4

1 回答 1

6

您根本不需要For Each循环,While循环将为您遍历阅读器,如下所示:

While reader.Read()
    Dim firstName = reader.GetString(0)
    Dim LastName = reader.GetString(1)
    Dim Address = reader.GetString(2)
    Dim City = reader.GetString(3)
    Dim State = reader.GetString(4)
    Dim Zip = reader.GetString(5)
    Dim Notes = reader.GetString(6)
    Dim Amount As Decimal = reader(7)
    Session("PDF") &= firstName & " " & LastName & Environment.NewLine & Address & Environment.NewLine & City & "," & State & " " & Zip & Environment.NewLine & "Consulting Fee Amount: " & Amount & Environment.NewLine & "Notes: " & Notes
End While
于 2013-09-24T17:51:03.967 回答