开发者

data reader in vb.net

开发者 https://www.devze.com 2023-01-07 19:19 出处:网络
Please help, how do I make a while loopequivalent of this for loop. So that I could read from one row in the table of mysql database and displayit on the combobox in vb.net.

Please help, how do I make a while loop equivalent of this for loop. So that I could read from one row in the table of mysql database and display it on the combobox in vb.net.

I use this code, but its definitely not useful if there are 3 or more开发者_StackOverflow中文版 items that are added in the row:

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

I want to read all the data from that the Drug_type row Please help, thanks


If you want to read only first row than just use

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

Update

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()


@pranay You don't need the nested loops.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号