开发者

How do I establish a connection to MySQL via VB & display simple query results on a form?

开发者 https://www.devze.com 2023-01-30 16:48 出处:网络
I have a project in Visual Studio 2008 and there is a working data connection to a MySQL database.In other words, I can query the db directly from Visual Studio and it will display the results.

I have a project in Visual Studio 2008 and there is a working data connection to a MySQL database. In other words, I can query the db directly from Visual Studio and it will display the results.

I've tried a couple of approaches that I found online for writing a connection string and accessing the db, but no luck yet.

All I'm trying to do is code a button to query the db and then reset the text property of a label/textbox to display the results based upon another label/textbox value.

The pseudo-code I am imagining is something like this:

    Private Sub query_submit_button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)开发者_运维百科 Handles query_submit_button.Click
        result_textbox.Text = SELECT field FROM table WHERE otherfield = key_textbox.Text
    End Sub

I didn't see any related questions posted on SO - forgive me if I missed one that already exists and this is a dupe.

What is the correct way to accomplish this?


EDIT

Using MySQL 5.1


You can download the ODBC Provider and then reference, import, and use it to query the database through a ODBCCommand instance.

'Put this at the very top of your .VB file...
Imports System.Data.ODBC

'Put this in some method in your code when you are ready to query the DB...
Using connection As New OdbcConnection(connectionString)
    Dim command As New OdbcCommand(strSqlQuery, connection)
    connection.Open()
    result_textbox.Text = command.ExecuteScalar.ToString
End Using


i used DaMartyr's answer to get 99% there, but needed to add this declaration:

Dim connectionString As String = "driver={MySQL ODBC 5.1 Driver};server=SERVER;uid=USERID;pwd=PASSWORD;database=DATABASE"

just replace the SERVER, USERID, PASSWORD, & DATABASE with your personal settings

0

精彩评论

暂无评论...
验证码 换一张
取 消