【问题标题】:How to use data reader in vb.net如何在 vb.net 中使用数据阅读器
【发布时间】:2013-02-18 11:34:15
【问题描述】:

请帮忙,我如何在 vb.net 中真正使用数据阅读器。我正在使用 odbc 连接 mysql 和 vb.net。
我在模块上声明的函数:

Public Function form2search(ByVal drugname As String) As OdbcDataReader

        cmd.CommandText = "SELECT *  FROM drug WHERE Drug_name LIKE'%" & drugname & "' "
        Return cmd.ExecuteReader

    End Function

text_changed 事件:

con.drugname=textBoxdrugname.text
     Dim rdr As Odbc.OdbcDataReader
            rdr = con.form2search(drugname)
        if rdr.hasrows=true then
        rdr.read()

        TextBoxdrugname.Text = rdr("Drug_name").ToString
                        TextBoxdrugcode.Text = rdr("Drug_code").ToString
                        drugtype.Text = rdr("Drug_type").ToString

        end if

我看到了一个结果,但它只加载了数据库中的第一项。我已将此代码放入 text_changed 事件中。这样做的正确方法是什么?第二个代码有什么问题,为什么只加载第一个数据

如您所见,con 是我声明函数的模块。然后我在表单中创建了它的一个对象。

【问题讨论】:

    标签: mysql database vb.net


    【解决方案1】:

    DataReader 是低级实现,不支持导航,每次调用时只读取一行

    reader.Read()
    

    对于 Windows 窗体应用程序,您可能应该使用 DataSet / DataTable 方法或 ORM。您应该考虑在 odbc 驱动程序上使用 mysql 连接器网络。它可以在 mysql.com 上找到。

    这是一个小演示代码:

    dim table as new DataTable("table1")
    
    ' Create a Connection
    using conn as new MysqlConnection("...connectionstring")
        conn.Open() ' Open it
    
        ' Create a new Command Object
        using cmd as new MysqlCommand("SELECT * FROM table", conn)
    
            ' Create a DataAdapter
            ' A DataAdapter can fill a DataSet or DataTable
            ' and if you use it with a CommandBuilder it also
            ' can persist the changes back to the DB with da.Update(...)
            using da as new MysqlDataAdapter(cmd)
                da.Fill(table) ' Fill the table
            end using
    
        end using
    end using
    
    ' A Binding Source allows record navigation
    dim bs as new BindingSource(table, nothing)
    
    ' You can bind virtually every property (most common are "text" "checked" or "visible"
    ' of a windows.forms control to a DataSource
    ' like a DataTable or even plain objects
    textBox1.DataBindings.Add("Text", bs, "columnName")
    
    ' Now you can navigate your data
    bs.MoveNext()
    
    ' Even a ComboBox can be bound to a List and display the related value
    ' of your current row
    comboBox1.DataSource = table2
    comboBox1.DisplayMember = "name"
    comboBox1.ValueMember = "id"
    
    comboBox1.DataBindings.Add("SelectedValue", bs, "id")
    

    【讨论】:

      【解决方案2】:

      你只需将数据读取器的对象放在while循环中。示例代码如下:

            dr = myCommand.ExecuteReader()
            While dr.Read()
            'reading from the datareader
             MessageBox.Show("colname1" & dr(0).ToString())
             MessageBox.Show("colname2" & dr(1).ToString())
             MessageBox.Show("colname3" & dr(2).ToString())
             MessageBox.Show("colname4" & dr(3).ToString())
             MessageBox.Show("colname5" & dr(4).ToString())
            'displaying the data from the table
             End While
             dr.Close()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-09
        • 1970-01-01
        • 2017-08-03
        • 2015-02-28
        • 2022-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多