djdonal3000
djdonal3000

Reputation:

Visual Basic 2005 + mysql

How do I connect to MySQL and get started with VB 2005? Been using PHP + MySQL in work for bout a year now. Just wondering how to get started with Visual Basic 2005 + MySQL. Just to connect to a database, and run a simple select star query?

db -> db_name
table -> tbl_name
ip -> localhost

Upvotes: 0

Views: 1427

Answers (1)

Andomar
Andomar

Reputation: 238086

You can use the ADO.NET Driver for MySQL (Connector/NET), which you can download here: http://www.mysql.com/products/connector/

After installing, you can use MySQL in the standard .NET way, using MySqlConnection, MySqlCommand, MySqlDataReader, and so on. The documentation is here: http://dev.mysql.com/doc/refman/5.0/en/connector-net-programming.html

Some example code:

Dim myConnection As New MySql.Data.MySqlClient.MySqlConnection
myConnection.ConnectionString = "server=127.0.0.1;" _
            & "uid=root;" _
            & "pwd=12345;" _
            & "database=test;"
myConnection.Open()
Dim myCommand As New MySqlCommand("select * from TheTable", myConnection)
Using myReader As MySqlDataReader = myCommand.ExecuteReader()
    While myReader.Read()
        Console.WriteLine((myReader.GetInt32(0) & ", " & myReader.GetString(1)))
    End While
End Using
myConnection.Close()

The Using statement makes sure the data reader is closed when you no longer need it, even if an exception is thrown. You could also enclose the connection in a Using statement.

Upvotes: 3

Related Questions