【问题标题】:How to read a value from mysql database?如何从mysql数据库中读取一个值?
【发布时间】:2019-10-19 07:38:13
【问题描述】:

我希望能够读取一个值(在本例中为组 ID)。我看过/阅读的所有主题和教程都将数据放入文本框中。

在这种情况下,我不想把它放在文本框中;我想获取 Group ID 然后说:

如果组 ID = 4 则登录

这是数据库的图像。

基本上,但我看的教程或多个论坛都没有。他们都没有接受一个值并说如果 value = 4 然后登录或做其他事情。

If text = "1" Then
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString =
        "server='ip of server'.; username=; password=; database="
    Dim READER As MySqlDataReader
    Dim member_group_id As String

    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "SELECT * FROM `core_members` where name='" & TextBox2.Text & "'"
        Query = "SELECT * FROM `nexus_licensekeys` where lkey_key='" & TextBox1.Text & "'"
        COMMAND = New MySqlCommand(Query, MysqlConn)
        READER = COMMAND.ExecuteReader
        Dim count As Integer
        count = 0
        While READER.Read
            count = count + 1
        End While

这是我目前所拥有的。我是一种使用 Visual Basic 实现 mysql 数据的新人,最近才开始涉足它。我不确定接下来会发生什么,甚至如何从阅读组 ID 等开始。

正如我所说,从这里开始的任何帮助都将非常感谢如何读取组 id 并说如果这个组 id = 这个数字然后做这个或那个。我相信你明白了。

【问题讨论】:

  • 你为什么要Query = "SELECT * FROM ...,然后又是Query = "SELECT * FROM ...
  • 如果你把它放入 TextBox2 然后运行会发生什么? ';drop table core_members;--
  • 总是使用参数来避免sql注入和格式化错误。
  • 您从网络上获取的代码您说它将值放入“textbox1”,对吧?而且您不希望它出现在文本框中,这就是您来问这个问题的原因?更改那部分而不是 textbox1.text = 不管,随便拿,现在你有了你想要的值。顺便说一句,你的第二个查询正在替换你的第一个查询,如果你只想要 groupID,为什么选择 *? dim count as integer = 0, count += 1,你必须在提问之前学习语言,来这里之前你没有办法搜索答案:)
  • @Blooberz 不,第二个分配会覆盖第一个分配。 SELECT * FROM core_members... 在您的示例中从未使用过

标签: mysql vb.net


【解决方案1】:

我将代码分为 UI Sub 和可以将数据返回到 UI 的 Data Access Function。您的事件过程代码应该相当简短,并且函数应该有一个目的。

将数据库对象保留在方法的本地。这样您可以更好地控制。 Using...End Using 块确保即使出现错误,您的数据库对象也会关闭和处置。

我留给你添加验证码。检查空文本框或不返回记录。

我希望这可以作为使用 ADO.net 的快速介绍。要点是:

  1. 使用参数

  2. 确保连接已关闭。 (使用块)

Private ConnString As String = "server=ip of server; username=; password=; database="

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim GroupID As String = GetGroupID(TextBox1.Text)
    If GroupID = "4" Then
        'your code here
    End If
    Dim LocalTable As DataTable = GetLicenseKeysData(TextBox1.Text)
    'Get the count
    Dim RowCount As Integer = LocalTable.Rows.Count
    'Display the data
    DataGridView1.DataSource = LocalTable
End Sub

Private Function GetGroupID(InputName As String) As String
    'Are you sure member_group_id is a String? Sure looks like it should be an Integer
    Dim member_group_id As String = ""
    'You can pass the connection string directly to the constructor of the connection
    Using MysqlConn As New MySqlConnection(ConnString)
        'If you only need the value of one field Select just the field not *
        'ALWAYS use parameters. See comment by @djv concerning drop table
        Using cmd As New MySqlCommand("SELECT g_id FROM core_members where name= @Name")
            'The parameters are interperted by the server as a value and not executable code
            'so even if a malicious user entered "drop table" it would not be executed.
            cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = InputName
            MysqlConn.Open()
            'ExecuteScalar returns the first column of the first row of the result set
            member_group_id = cmd.ExecuteScalar.ToString
        End Using
    End Using
    Return member_group_id
End Function

Private Function GetLicenseKeysData(InputName As String) As DataTable
    Dim dt As New DataTable
    Using cn As New MySqlConnection(ConnString)
        Using cmd As New MySqlCommand("SELECT * FROM `nexus_licensekeys` where lkey_key= @Name;", cn)
            cmd.Parameters.Add("@Name", MySqlDbType.VarChar).Value = InputName
            cn.Open()
            dt.Load(cmd.ExecuteReader())
        End Using
    End Using
    Return dt
End Function

【讨论】:

  • @djv 谢谢。修好了。
猜你喜欢
  • 2020-11-26
  • 2012-03-13
  • 2019-11-08
  • 2013-03-31
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 2011-11-02
相关资源
最近更新 更多