[Update: Go to this new post instead on how to use MySQL ADO.NET drivers]

This is a three step description of what you need to do to access a MySQL server from VB.NET.

I used Microsoft Windows Server 2003 with Microsoft Visual Studio 2003 Professional and MySQL Server 4.1.7.

It is possible to use ODBC, OLEDB and native .NET providers to access MySQL from .NET. However the ODBC driver (MyODBC) is the only official driver (ie from MySQL) provided for free. The drawback is that ODBC gives the worst performance of the three. More info on this can be found at dev.mysql.com/tech-resources/articles/dotnet.

Ok, so this is how I did it:

1) Download and install the MyODBC driver. Latest version can be found at dev.mysql.com/downloads/connector/odbc. Current version, used in this example, is 3.51.

2) Start a new or use an existing ASP.NET (using VB.NET) project in Visual Studio. You need to import the System.Data.Odbc namespace to the project. From the “Project” menu select “Properties”. Under “Common Properties” and “Imports” type “System.Data.Odbc” in the “Namespace” box and click “Add Import”.

3) Below is some example code to get started. More examples can be found at dev.mysql.com/doc/mysql/en/odbc-net-op-vb-cp.html.

Dim MyConString As String = _
"DRIVER={MySQL ODBC 3.51 Driver};" & _
"SERVER=localhost;" & _
"DATABASE=testdb;" & _
"UID=root;" & _
"PASSWORD=rootpassword;" & _

Dim MyConnection As New OdbcConnection(MyConString)

Dim MyCommand As New OdbcCommand
MyCommand.Connection = MyConnection

Happy programming!

(This article is previously published and has been moved to this blog)