0

请帮忙,我如何制作一个相当于这个for循环的while循环。这样我就可以从 mysql 数据库表中的一行读取并将其显示在 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 行读取所有数据请帮助,谢谢

4

2 回答 2

3

如果您只想读取第一行,而不仅仅是使用

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()
于 2010-07-16T07:58:38.770 回答
2

@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
于 2010-07-16T09:44:23.133 回答