2013-09-24 69 views
1

我正在嘗試爲while語句中的每個語句執行一次,並且它不會在每個語句中執行我的代碼。我假設「價值」不是以某種方式分配的?對於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 

回答

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 
+0

謝謝你,我的壞我上面說的一些代碼被修改while語句之前的參考號正在執行。 – user1342164