VB.NET:无法使用MySQL Data Reader dr.HasRows读取数据库记录

时间:2014-07-23 08:09:02

标签: mysql vb.net

当我在文本框中单击带有有效ID号的按钮2时,它始终显示消息框“无效的ID号”但是,如果我删除IF语句,它显示数据库记录,它工作正常,但我需要这个IF语句,我认为这里的问题是dr.HasRows,但我不知道放什么。

Imports MySql.Data.MySqlClient

Public Class Form16
    Private Sub Form16_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim con As New MySqlConnection("server=localhost;user id=root;password=root;database=db")
        Dim DataSet1 As New DataSet
        Dim dr As MySqlDataReader
        Dim da As New MySqlDataAdapter
        Dim cmd As New MySqlCommand

        con.ConnectionString = "server = localhost; user id = root;password=root; database = db"
        cmd.Connection = con
        con.Open()
        cmd.CommandText = "select * from voter where idn='" & TextBox1.Text & "'"
        dr = cmd.ExecuteReader
        con.Close()
        da.SelectCommand = cmd
        da.Fill(DataSet1, "db")


        If dr.HasRows Then
            Label2.DataBindings.Add("text", DataSet1, "db.fname")
            Label10.DataBindings.Add("text", DataSet1, "db.mi")
            Label11.DataBindings.Add("text", DataSet1, "db.lname")
            Label12.DataBindings.Add("text", DataSet1, "db.yr")
            Label13.DataBindings.Add("text", DataSet1, "db.sec")
            Label14.DataBindings.Add("text", DataSet1, "db.vstatus")
        Else
            MessageBox.Show("Invalid ID No.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        Label2.DataBindings.Clear()
        Label10.DataBindings.Clear()
        Label11.DataBindings.Clear()
        Label12.DataBindings.Clear()
        Label13.DataBindings.Clear()
        Label14.DataBindings.Clear()
    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

你已经完成了比以往更多的工作......如果你打算使用datareader,你的代码最终会看起来像这样。 (我没有测试过这段代码)

Imports MySql.Data.MySqlClient

Public Class Form16
    Private Sub Form16_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim con As New MySqlConnection("server=localhost;user id=root;password=root;database=db")
    Dim DataSet1 As New DataSet
    Dim dr As MySqlDataReader
    Dim da As New MySqlDataAdapter
    Dim cmd As New MySqlCommand

    con.ConnectionString = "server = localhost; user id = root;password=root; database = db"
    cmd.Connection = con
    con.Open()
    cmd.CommandText = "select * from voter where idn='" & TextBox1.Text & "'"
    dr = cmd.ExecuteReader
    con.Close()


     if dr.read then

        Label2.text = dr("fname") 
        Label10.text = dr("mi")
        Label11.text = dr("lname")
        Label12.text = dr("yr") 
        Label13.text = dr("sec")
        Label14.text = dr("vstatus")

     else
        MessageBox.show("Invalid ID Number")
     endif

End Class

答案 1 :(得分:0)

您需要使用Parameterized查询来阻止SQL Injection

Dim commandText as String = "SELECT * FROM Voter WHERE idn=@idn"
Dim command As New MySqlCommand(commandText, connection)

command.Parameters.AddWithValue("@idn", TextBox1.Text)

如果您使用DataSet,则无需使用DataAdapterDataReader,因为您可以将DataReader转换为DataTable:< / p>

dr = command.ExecuteReader() ' Get Data Reader Rows
dt.Load(dr) 'Convert DataReader into DataTable

现在可以绑定到您的LabelTextBox

Label2.DataBindings.Add("Text", dt, "fname")

您不需要使用HasRows属性来检查DataReader是否有行,而是可以查看Row Count的{​​{1}}:

DataTable

我也使用dotNet中的If (dt.Rows.Count > 0) Then Label2.DataBindings.Add("Text", dt, "fname") End If 语句专门用于连接,这样你就不用关闭了:

Using

检查下面的完整代码:

 Using connection As New MySqlConnection(connectionString)
    'More code here
 End Using ' Close the connection automatically