2010-02-23 72 views
0

我陷入了這個問題。這似乎很簡單,但由於某種原因我有麻煩。SQL查詢綁定到列表框的結果

這是我有以下幾點:

Try 
     cn = New OleDbConnection("Provider=microsoft.Jet.OLEDB.4.0;Data Source=G:\Sean\BMSBonder3_0.mdb;") 
     cn.Open() 
     str = "Select Distinct BonderIdentifier From [Session]" 
     cmd = New OleDbCommand(str, cn) 
     dr = cmd.ExecuteReader 

     dr.Read() 
     If dr.Item(0).ToString <> "" Then 
      ListBox1.Items.Add(dr.Item(0)) 
     End If 

     cn.Close() 
    Catch ex As Exception 
     MsgBox(ex.Message) 
    End Try 

這工作,僅保留值之一。其實是最後一個。我怎麼能得到他們所有的人?

對不起,新手問題。搜索沒有太多幫助。

回答

2

您需要使用While循環重複執行您的代碼,直到dr.Read()返回False
例如:

While dr.Read() 
    If dr.Item(0).ToString <> "" Then 
     ListBox1.Items.Add(dr.Item(0)) 
    End If 
Wend 
+0

哦,這就是正確的...謝謝...對不起,愚蠢的問題 – 2010-02-23 21:50:43