MySQL 从mysql中选择放入变量VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16027925/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 17:21:21  来源:igfitidea点击:

Select from mysql put into variable VB.NET

mysqlvb.net

提问by Vinra Gunanta Pandia

I am trying to Select data from MySQL database using VB.NET

我正在尝试使用 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 & "'" 

but i dont know how to put selected query into variable, anybody can help me ?

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

采纳答案by Damith

you can use ExecuteScalar method as below

您可以使用 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

Note :

笔记 :

Better to use parameters when you call database, like below

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

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

then you have to add the parameter as

那么你必须将参数添加为

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

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

如何创建参数化 SQL 查询?我为什么要?

回答by Androidz

 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

Using the Data Reader it will let you assign the result of the query to your variable StrVar and this will come in handy. I use GetString because I assume it is a string type and GetValue for integer. The value "1" represent the column you want to pass to your variable.

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

Let me know if this works. Cheers..Happy Coding..

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

回答by Ruly

You can put it into DataSet

你可以把它放进 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

From above command, your data will be stored in a DataSet.

从上面的命令,您的数据将存储在一个DataSet.

Sample to use:

使用示例:

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

You can check MSDN for further information:

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

DataSet: http://msdn.microsoft.com/en-us/library/system.data.dataset.aspx

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

DataTable: http://msdn.microsoft.com/en-sg/library/system.data.datatable.aspx

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

DataRow: http://msdn.microsoft.com/en-sg/library/system.data.datarow.aspx

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

Note:

笔记:

As mentioned by @Joel Coehoorn, try to look at Command Parameter http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqlparameter.html

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