对于未在While语句中执行的每个语句?

时间:2013-09-24 17:45:36

标签: asp.net vb.net

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

1 个答案:

答案 0 :(得分: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