tags:

views:

103

answers:

2

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? Any helpers, or sample code welcome...

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

cheers lads!

+3  A: 

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.

Andomar
A: 

Exactly wat i was looking for. Cheers bro.