Accessing MySQL on VB.NET using MySQL Connector/Net, Part VI: Create Connection
Programming, VB.NET March 13th, 2009Create Connection
After I have prepared many things for showing how to access MySQL Server using VB.NET. Let’s see what I have done so far. Right now, I have a remote MySQL Server at 192.168.125.21 with port 3306 (Default port). Also, a sample database “world” and a user account “worldUser” with password “worldpassword”. Now it’s time to start Microsoft Visual Studio 2005 on a development PC.
You can see index of this series at Accessing MySQL on VB.NET using MySQL Connector/Net, Part I: Introduction
Create Connection
- On Development PC, open Microsoft Visual Studio 2005.

- Create a New Windows Application Project “SampleMySQL”.

- First, I need to add a MySQL library. Right-click on the project name (SampleMySQL) -> Add Reference.

- On Add Reference, select MySQL.Data on .NET tab.

- By default, the reference library (MySQL.Data) won’t be copied to the output directory. That means when you deploy the application on other PC which doesn’t have the library installed, it’ll throw error. So I have to set the Copy Local property of the library file to True. Click Show All Files icon.

- Expand References -> Select MySQL.Data -> Change Copy Local property to True.

- Now it’s time to coding the application. First, I have to import a namespace. Open the Code View and add this line on the top.
Imports MySql.Data.MySqlClient
- Add these code to the Class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load TestConnection() End Sub Public Sub TestConnection() Try Dim connStr As String = "Database=world;" & _ "Data Source=192.168.125.21;" & _ "User Id=worldUser;Password=worldpassword" Dim connection As New MySqlConnection(connStr) connection.Open() connection.Close() MsgBox("Connection is okay.") Catch ex As Exception MsgBox(ex.Message) End Try End Sub
Code Explanation:
- Line 1-4: Simple Form_Load event that call TestConnection() method. The method is invoked when the form is loaded.
- Line: 7-17: Try-Catch scope. If there is any error in try scope, throws exception and goes to catch scope.
- Line: 8-10: A connection string represents configuration for connect to MySQL Server. Common attributes are:
- Database. The database to be used after a connection is opened.
- Data Source. The name of the MySQL server to which to connect.
- Port. The port MySQL is using to listen for connections. The default value is 3306
- User ID. The user that use to connect to the database on MySQL Server
- Password. Password of the user.
- Connection Timeout. Time to wait while trying to establish a connection before terminating the attempt and generating an error.
- Line 11: Create MySqlConnection object and assign connectionString property.
- Line 12-13: Test open and close the connection to the database on MySQL Server.
- Line 14: If there is no error, show a success message.
- Line 16: Show the error message.
- Next, test the code by run the application. If the connection is successfully connected and disconnected. You’ll see the message “Connection is okay”.

- If something wrongs, you’ll see message other than the previous step. The figure below is the example that mistyped the database name in the connection string.

Related post
- Accessing MySQL on VB.NET using MySQL Connector/Net, Part IV: Create & Grant MySQL User Account Create & Grant MySQL User Account By default, the root account on MySQL Server has all privileges on every tables...
- Accessing MySQL on VB.NET using MySQL Connector/Net, Part V: Install MySQL Connector Net Install MySQL Connector Net From last 4 parts, I have prepared MySQL Server with sample database. I’ve done on a...
- Accessing MySQL on VB.NET using MySQL Connector/Net, Part VII: Perform SQL Operations Perform SQL Operations From the previous part, I have successfully connect to world database on MySQL Server from VB.NET. Next,...
- Accessing MySQL on VB.NET using MySQL Connector/Net, Part VIII: Display Result on GUI Introduction Actually, I don’t prepare to write this post while I was writing the series. But there are some people...
- Accessing MySQL on VB.NET using MySQL Connector/Net, Part I: Introduction Introduction Here comes again, tutorial about programming to access a database server. This tutorial shows you how to use Microsoft...
Related posts:






May 27th, 2009 at 6:53 pm
Hello,
Thanks for the tutorial. It worked great.
October 21st, 2009 at 10:37 pm
Thanks
What i have to do to connect using the ODBC Connector for .NET???
October 26th, 2009 at 9:42 am
Hi, Fernando
You can use MySQL Connector/Net by follow this topic to connect to MySQL database on .NET application.
October 28th, 2009 at 10:58 am
Hai Bro,
Thanks for the information….. very helpfull
October 29th, 2009 at 9:58 am
i have problem, pocket pc proyect
error 1
Es necesaria una referencia al ensamblado ‘System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′ que contenga la clase base ‘System.Data.Common.DbConnection’. Agregue una al proyecto.
i need help
November 4th, 2009 at 9:42 am
Hi, Jose Miguel Castillo
Did you follow the example above? The error message tell to add different reference library from my sample above. Try to add the reference System.Data to your project and re-build it again.
November 13th, 2009 at 11:24 am
Thanks for ur tute.gradefull
January 29th, 2010 at 9:18 am
i love this site.
March 5th, 2010 at 9:29 am
hi, im having this error, when trying to access to mysql remote server
Access denied for user ‘root’@'%’ to database ‘myDatase’
- i’m using vb.net express edition 2008
- mysql connector 6.1.3
is there an issue when i have mysql in my development machine where im doing my vb project?
thanks
March 5th, 2010 at 10:56 am
hi, its ok now… i skipped the first part about GRANT, i re-read chapter 4 and found what i’ve missed
thanks for the tutorials
March 20th, 2010 at 4:39 am
I must say; this is really a good tutorial! I tried many different ones but this was very well explained.
Regards,
Derek
June 17th, 2010 at 10:02 am
it works for my application!! Thank u so much =)
August 16th, 2010 at 1:08 pm
Hi, i just want to know how to cnnect MySQL from VB.net2003?
August 19th, 2010 at 9:05 pm
hey
this is a good example . i got connected with vb.net
thanks
anand