繁体   English   中英

VB.NET MySQL 查询不返回任何值

[英]VB.NET MySQL query returns no value

我试图在 vb.net 中使用 MySQL 在组合框中显示值。 现在我面临的问题是组合框没有显示来自 MySQL 的值。 我有以下代码:

MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=s974_db"

    Try
        MySqlConn.Open()
        Label21.Text = "DB Connection Successful"

        Dim Query As String
        Query = "select * from s974_db.processors where Name='" & ComboBox1.Text & "'"

        COMMAND = New MySqlCommand(Query, MySqlConn)
        READER = COMMAND.ExecuteReader
        While READER.Read
            Label10.Text = READER.GetDouble("Price")

        End While


        MySqlConn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()


    End Try

但是,使用上面的代码 Combobox1.Text 不返回任何内容,但是如果我使用以下具有不同查询的代码,它可以工作:

        MySqlConn = New MySqlConnection
    MySqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=s974_db"

    Try
        MySqlConn.Open()
        Label21.Text = "DB Connection Successful"

        Dim Query As String
        Query = "select * from s974_db.processors"

        COMMAND = New MySqlCommand(Query, MySqlConn)
        READER = COMMAND.ExecuteReader
        While READER.Read
            Dim sName = READER.GetString("Name")
            ComboBox1.Items.Add(sName)
        End While


        MySqlConn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MySqlConn.Dispose()


    End Try

有人可以检查一下并让我知道可能是什么问题吗? 谢谢!

所以我在评论中提到的亮点

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' You need only to open aconnection once
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=s974_db"

Try
  MySqlConn.Open()
  Label21.Text = "db connection successful"
  'First load both Combobox
  Dim query As String
  query = "select * from s974_db.processors"

  COMMAND = New MySqlCommand(query, MySqlConn)
  READER = COMMAND.ExecuteReader
  While READER.Read
    Dim sname = READER.GetString("name")
    ComboBox1.Items.Add(sname)
    ComboBox2.Items.Add(sname)
  End While



Catch ex As MySqlException
  MessageBox.Show(ex.Message)
Finally


End Try
End Sub
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Closing
  Try
    MySqlConn.Close()
    MySqlConn.Dispose()
  Catch ex As Exception

  End Try
 End Sub

现在是组合框

 Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboBox1.SelectedIndexChanged
'Only when  there is a item selected , ask for data
If ComboBox1.SelectedIndex > -1 Then
  Try

    Dim Query As String
    Query = "select * from s974_db.processors where Name='" & ComboBox1.Text & "'"

    COMMAND = New MySqlCommand(Query, MySqlConn)
    READER = COMMAND.ExecuteReader
    While READER.Read
      Label11.Text = READER.GetDouble("Price")

    End While



  Catch ex As MySqlException
    MessageBox.Show(ex.Message)
  Finally


  End Try
End If
End Sub

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged
If ComboBox2.SelectedIndex > -1 Then
  Try

    Dim Query As String
    Query = "select * from s974_db.processors where Name='" & ComboBox1.Text & "'"

    COMMAND = New MySqlCommand(Query, MySqlConn)
    READER = COMMAND.ExecuteReader
    While READER.Read
      Label10.Text = READER.GetDouble("Price")

    End While



  Catch ex As MySqlException
    MessageBox.Show(ex.Message)
  Finally


  End Try
End If
End Sub

这与我在 Form_load 上描述的完全一样,您填充了两个组合框

当您现在更改其中一个组合框时,其中一个标签也会更改。

有时您必须更新 Element 才能看到更改

在这种情况下,你写在循环的末尾

Label10.Update()

从顶部开始... 将数据库对象保持在使用它们的方法的本地。 (不是表单级变量)您可以使连接字符串成为类级字符串变量。 这是您可以确保它们被关闭和处理的唯一方法。

Using...End Using块将关闭并处理您的数据库对象,即使出现错误也是如此。 连接的构造函数采用连接字符串。 连接是宝贵的对象。 .Execute方法之前不要打开连接并尽快关闭它。

用户可以在Form.Load之前从ComboBox1选择一个项目没有多大意义。

一般来说,我们不想下载不必要的数据,我们希望尽可能少地访问数据库。 Form.Load我们将组合框绑定到包含名称和价格字段的数据表,设置显示和值成员。 现在,只要用户在组合中选择一个名称,我们就可以检索价格而无需再次连接到数据库。

我注意到你在另一个事件中使用了Val 这是一个旧的 VB6 方法,可以给你意想不到的结果。 .Net 和 vb.net 有各种各样的方法可以更快、更可靠地从字符串中获取数字。 CInt.TryParse.ParseCTypeConvert.To等。

Public Class Form1

    Private ConString As String = "server=localhost;userid=root;password=root;database=s974_db"

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Fill combobox
        Dim dt As New DataTable
        Using cn As New MySqlConnection(ConString),
                cmd As New MySqlCommand("select Name, Price from processors;", cn)
            cn.Open()
            dt.Load(cmd.ExecuteReader)
        End Using 'Closes and disposes both connection and command
        ComboBox1.DataSource = dt
        ComboBox1.DisplayMember = "Name"
        ComboBox1.ValueMember = "Price"
    End Sub

    Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
        Label10.Text = ComboBox1.SelectedValue.ToString
        ClearLabels()
    End Sub

    Private Sub ClearLabels()
        Label11.Text = ""
        Label12.Text = ""
        Label13.Text = ""
        Label14.Text = ""
        Label15.Text = ""
        Label16.Text = ""
        Label17.Text = ""
        Label18.Text = ""
        Label19.Text = ""
        Label20.Text = ""
    End Sub

End Class

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM