2010-07-16 46 views
0

請幫忙,我該如何做一個while循環等效於這個for循環。這樣我就可以讀取mysql數據庫表中的一行,並將其顯示在vb.net的組合框中。vb.net中的數據閱讀器

我使用此代碼,但它絕對不是有用的,如果有3名或更多的項目被列在說:

Dim i As Integer 
     Dim rdr As Odbc.OdbcDataReader 
     rdr = con.readfrom_drug_type_table() 
    For i = 0 To 1 
     If rdr.HasRows = True Then 
      rdr.Read() 

      ComboBox2.Items.Add(rdr("Drug_type")) 
     End If 
    Next i 

我想讀的Drug_type行 請幫助所有的數據感謝

回答

2

如果你想只讀第一行並非只是使用

If rdr.Read() Then 
    ComboBox2.Items.Add(rdr("Drug_type")) 
End If 

更新

Try 
    myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs") 
    'you need to provide password for sql server 
    myConnection.Open() 
    myCommand = New SqlCommand("Select * from discounts", myConnection) 
    dr = myCommand.ExecuteReader 

     While dr.Read() 
      WriteLine(dr(0)) 
      WriteLine(dr(1)) 
      WriteLine(dr(2)) 
      WriteLine(dr(3)) 
      WriteLine(dr(4)) 
      ' writing to console 
     End While 
Catch 
End Try 
dr.Close() 
myConnection.Close() 
+0

我想讀從Drug_type行的所有數據現在 – user225269 2010-07-16 08:32:35

+0

檢查updaetd答案 – 2010-07-16 09:02:50

+0

謝謝,但不起作用,看來你只能複製和從其他地方粘貼它。正如你可以看到我使用odbc連接到mysql數據庫。我不使用mysql connector.net – user225269 2010-07-16 10:17:15

1

@pranay 您不需要嵌套循環。

Try 
    myConnection = New SqlConnection("server=localhost;uid=sa;pwd=;database=pubs") 
    myConnection.Open() 
    myCommand = New SqlCommand("Select * from discounts", myConnection) 
    dr = myCommand.ExecuteReader() 
    While dr.Read() 
     WriteLine(dr(0)) 
     WriteLine(dr(1)) 
     WriteLine(dr(2)) 
     WriteLine(dr(3)) 
     WriteLine(dr(4)) 
    End While 
    dr.Close() 
Finally 
    myConnection.Close() 
End Try