how to retrieve mysql data in vb.net?

2019-04-13 07:20发布

im trying to retrieve mysql data with specific column and show to textbox in vb.net. what should i do in retrieving it?

Dim connect As New MySqlConnection("server=localhost; user id=root; password= ; database=ticketing_system;")
    connect.Open()

    Dim sqladapter As New MySqlDataAdapter
    Dim sqlcmd As New MySqlCommand
    Dim dr As MySqlDataReader
    Dim dt As New DataTable

    sqlcmd = New MySqlCommand("SELECT * complaint WHERE tran_no='" & lbltranno.Text & "'")
    **THEN? WHAT SHOULD I DO TO DISPLAY DATA? PLEASE HELP**

    connect.Close()

8条回答
【Aperson】
2楼-- · 2019-04-13 08:01

You can try this:

Dim connString As String = "server=localhost; user id=root; password= ; database=ticketing_system;"
Dim sqlQuery As String = "SELECT * complaint WHERE tran_no='" & lbltranno.Text & "'";
Using sqlConn As New MySqlConnection(connString)
    Using sqlComm As New MySqlCommand()
        With sqlComm
            .Commandtext = sqlQuery
        End With
        Try
            sqlConn.Open()
            Dim sqlReader As MySqlDataReader = sqlComm.ExecuteReader()
            While sqlReader.Read()
                Label1.Text = sqlReader("Name").ToString()
                Label2.Text = sqlReader("Points").ToString()
            End While
        Catch ex As MySQLException
            ' add your exception here '
        End Try
    End Using
End Using
查看更多
Rolldiameter
3楼-- · 2019-04-13 08:05
    Dim MysqlConn As MySqlConnection
    Dim connectionString As String = "Server=-host-ip-;Database=Mysqldb;Uid=user;Pwd=password"
    MysqlConn = New MySqlConnection(connectionString)
    Try

        Dim Mysqlcmd As New MySqlCommand("SELECT * FROM Mysqldb.table WHERE col='" & Label6.Text & "'", MysqlConn)
        Dim dt As New DataTable
        Dim Mysqladapter As New MySqlDataAdapter()
        MysqlConn.Open()
        Mysqladapter.SelectCommand = Mysqlcmd

        Mysqladapter.Fill(dt)

        DataGridView1.DataSource = dt
        MessageBox.Show("Connection to Database has been opened.")

        MysqlConn.Close()

    Catch myerror As MySqlException

        MessageBox.Show("Cannot connect to database: " & myerror.Message)

    Finally

        MysqlConn.Dispose()

    End Try

The key is to add the connection at the end of the mysql query as in line 6

查看更多
登录 后发表回答