【问题标题】:Select from mysql put into variable VB.NET从mysql中选择放入变量VB.NET
【发布时间】:2013-04-08 07:40:25
【问题描述】:

我正在尝试使用 VB.NET 从 MySQL 数据库中选择数据

Dim conn As New MySqlConnection
    Dim cmd As New MySqlCommand
    conn.ConnectionString = "Server=localhost; user id=root; password=; database=aplikasi_store_testing;"
    cmd.Connection = conn
    conn.Open()

    Dim Number As Integer
    cmd.CommandText = "SELCECT nama_student  FROM student where Id_student ='" & id & "'" 

但我不知道如何将选定的查询放入变量中, 有人可以帮助我吗?

【问题讨论】:

  • 您的代码容易受到 sql 注入的影响。使用这样的字符串连接将值替换为 sql 查询是不行的。
  • 哦,是吗?您对此有什么建议吗?
  • 谷歌“参数化查询”。学习他们。使用它们。

标签: mysql vb.net


【解决方案1】:
 Dim StrVar as String
 Dim rd As MySqlDataReader 
 Dim cmd as New MySqlcommand


 cmd.commandtext = "Select student_name from student_table where student_id = @ID"
 cmd.connection = conn
 rd = cmd.ExecuteReader

if rd.read then

    StrVar = rd.GetString(1)

end if
rd.close

使用数据阅读器,您可以将查询结果分配给变量 StrVar,这将派上用场。我使用 GetString 是因为我假设它是字符串类型,而 GetValue 是整数。值“1”表示您要传递给变量的列。

让我知道这是否有效。干杯..快乐编码..

【讨论】:

    【解决方案2】:

    你可以使用下面的 ExecuteScalar 方法

    object nama_studentObj = cmd.ExecuteScalar()
    if nama_studentObj != null then
      string nama_student= nama_studentObj .ToString()
    

    Full example code

        Dim cs As String = "Database=testdb;Data Source=localhost;" _
            & "User Id=testuser;Password=test623"
    
        Dim stm As String = "SELECT VERSION()"
        Dim version As String
        Dim conn As MySqlConnection
    
        Try
            conn = New MySqlConnection(cs)
            conn.Open()
    
            Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)
    
            version = Convert.ToString(cmd.ExecuteScalar())
    
            Console.WriteLine("MySQL version: {0}", version)
    
        Catch ex As MySqlException
            Console.WriteLine("Error: " & ex.ToString())
        Finally
            conn.Close()
        End Try
    

    注意:

    调用数据库时最好使用参数,如下所示

    cmd.CommandText = "SELCECT nama_student  FROM student where Id_student = @Id_student"
    

    那么你必须将参数添加为

    cmd.Parameters.AddWithValue("Id_student", id )
    

    How do I create a parameterized SQL query? Why Should I?

    【讨论】:

    • 不是“AddWithValue”的粉丝。它必须推断 sql 类型。有时它会出错,更喜欢 nvarchar 而不是 varchar、nchar 或 char,这可能会破坏索引的使用并强制在表中的每一行中进行转换,从而导致糟糕的性能。
    【解决方案3】:

    你可以把它放到DataSet

    Dim conn As New MySqlConnection
    Dim cmd As New MySqlCommand
    conn.ConnectionString = "Server=localhost; user id=root; password=; database=aplikasi_store_testing;"
    cmd.Connection = conn
    conn.Open()
    
    Dim id As Integer
    cmd.CommandText = "SELECT nama_student  FROM student where Id_student ='" & id & "'" 
    
    Dim da As New MySqlDataAdapter 'DataAdapter can be used to fill DataSet
    Dim ds As New DataSet
    da.SelectCommand = cmd
    da.Fill(ds, "student") 'you can change student with the table name
    

    通过上述命令,您的数据将存储在DataSet

    使用示例:

    ds.Tables("student").Rows.Count 'Get the number of rows in the DataTable
    ds.Tables("student").Rows(0).Item("nama_student").ToString 'Get first row of the nama_student field
    

    您可以查看 MSDN 以获取更多信息:

    数据集:http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx

    数据表:http://msdn.microsoft.com/en-sg/library/system.data.datatable.aspx

    数据行:http://msdn.microsoft.com/en-sg/library/system.data.datarow.aspx

    注意:

    正如@Joel Coehoorn 所说,请尝试查看命令参数http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlparameter.html

    【讨论】:

    • 感谢您的帮助,但我认为手动连接到 MySQL 会让口味不同
    • 没有“手动连接 MySQL”,连接数据库的方式取决于您的需要。如果您觉得顺带一读对您更好,那就用它吧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-22
    • 1970-01-01
    • 2016-01-07
    • 2013-04-22
    • 2015-03-02
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多