vb.net 中的数据读取器

发布于 2024-09-10 07:21:47 字数 443 浏览 1 评论 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 行中的所有数据 请帮忙,谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回忆凄美了谁 2024-09-17 07:21:47

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

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

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()
扭转时空 2024-09-17 07:21:47

@普拉奈
您不需要嵌套循环。

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

@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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文