简体   繁体   English

如何从mysql数据库vb.net获取特定数据

[英]How to get a specific data from mysql database vb.net

My code searches for the same value of the selected item of a listview in the database for example: "01" the code searches for "01" in the database, now 01 in the database is equivalent to a name for example 01 = Name, my problem is; 我的代码在数据库中搜索列表视图中所选项目的相同值,例如:“ 01”,代码在数据库中搜索“ 01”,现在数据库中的01等同于名称,例如01 = Name,我的问题是 If the code founds 01 i want to get the name instead of the 01. 如果代码找到01,我想获取名称而不是01。

My Code 我的密码

     Try
        Call DatabaseConnection()
        MySqlConn.Open()
        For Each item As ListViewItem In ListViewAttendance.SelectedItems
            Query = "select * from dtr_database.dtr_entries where dtr_entry_number= '" & item.SubItems(0).Text & "'"
            Command = New MySqlCommand(Query, MySqlConn)
            Reader = Command.ExecuteReader
            Dim Count As Integer
            Count = 0
            While Reader.Read
                Count = Count + 1
            End While
            If Count = 1 Then

                'if 01 is found Get Name of 01. How to do this?

                MessageBox.Show("Record Found")
            ElseIf Count > 1 Then
                MessageBox.Show("Multiple Records Found")
            Else
                MessageBox.Show("Record Found2")
            End If
        Next
        MySqlConn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

Although your code has many issues, the direct answer to your question is that you should call Reader.GetString(ColumnNumber) inside the While loop to get the value of Name column. 尽管您的代码有很多问题,但直接的问题答案是您应该在While循环内调用Reader.GetString(ColumnNumber)以获取Name列的值。

However, there are a few things you could improve: 但是,您可以改进以下几点:

  1. First and the foremost, you should not execute queries directly by concatenating strings except for the most trivial applications. 首先,除了最琐碎的应用程序之外,您不应该直接通过连接字符串直接执行查询。 This can lead to all sorts of troubles, eg not the least of which is SQL Injection . 这可能会导致各种各样的麻烦,例如,至少是SQL Injection
  2. Do not use SELECT * in your queries. 不要在查询中使用SELECT * This will bring in all of the columns of your table whether you need them in the current scenario or not. 无论当前场景是否需要它们,这都会带入表的所有列。 Also, you'll not be sure of the order of the columns. 此外,您将不确定列的顺序。 Instead specify the column names you need in your query. 而是指定查询中需要的列名称。
  3. Go for ADO.NET DataSets or EF if possible. 如果可能,请使用ADO.NET数据集或EF。 They can create easy-to-use DataAdapters and wrapper methods for your queries that work just like standard vb.net functions. 他们可以为查询创建易于使用的DataAdapter和包装器方法,就像标准vb.net函数一样。 Plus they use command parameters to safeguard you against the odds of SQL injection. 另外,他们使用命令参数来保护您免受SQL注入的威胁。
  4. If you are inclined towards directly using SqlCommand in your code, try using ExecuteScalar() if you simply need to fetch a single column (Name column in your case). 如果您倾向于直接在代码中使用SqlCommand ,则仅需要获取单个列(本例中为Name列ExecuteScalar()时,请尝试使用ExecuteScalar() )。 That will save you from having to run the Read() loop. 这将使您不必运行Read()循环。

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

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