Connecting MySQL Database using Visual Basic.NET

In this tutorial I am going to show you the process on how to connect Basic using MySQL Database. To begin with, you need to download the MySQL Connector/NET first from this link http://dev.mysql.com/downloads/connector/odbc/. After downloading, you need to install it so the connector will now available to use under the Reference. Now let’s begin creating our application using VB.NET (I’m Using VB Express 2008 edition). And this time you need to add a reference using MySQL Connector. On the solution explorer, right click the project menu and choose “Add Reference” and click the Browse Tab and browse the installation folder where the connector was installed. (i.e. ”MySQL.Data.dll”) To have a reference to “System.Data.dll”. Now Import the Connector/Net to use its namespace.
  1. Imports MySql.Data.MySqlClient
  2. Public Class VBMYSQL
Now let’s design the form by adding two Buttons and name the first button as “Open Connection” and the other button is “Close Connection”. The form should look same in the image below. This time let’s start adding code to “Open Connection” button. To add code, just double click this button. Then we need to add the MySQLConnection object under Public class. It will look like the code below.
  1. Imports MySql.Data.MySqlClient
  2.  
  3. Public Class VBMYSQL
  4. Dim MysqlConn As New MySqlConnection
Next were going to set the Connection String (same on MS Access connection string). For example this is my Connection sting:
  1. MysqlConn.ConnectionString = ("server=localhost;user id=root;password=;database=test")
After setting up our connection string, we can now add our next line of code. This code will simply open the connection to our database. But following the good programming habit we need to put some error trapping if something wrong occur in our program. So we will use Try, Catch and finally method. For Open Connection button under ConnectionString just add this code:
  1. Try
  2.  
  3. MysqlConn.Open()
  4.  
  5. MessageBox.Show("Connection to Database has been opened.")
  6.  
  7. Catch myerror As MySqlException
  8.  
  9. MessageBox.Show("Cannot connect to database: " & myerror.Message)
  10.  
  11. Finally
  12.  
  13. MysqlConn.Dispose()
  14.  
  15. End Try
The code above is used for opening a connection and when the program detects that the connection is successfully opened, the program will show a message box saying “Connection to Database has been opened."), else if something went wrong the program will catch the error and display it using the MessageBox function, and Finally MysqlConn.Dispose() will free the resources used. Next, add this code in “Close the Connection” button:
  1. Try
  2. MysqlConn.Close()
  3.  
  4. MessageBox.Show("Connection to Database has been Closed.")
  5.  
  6. Catch myerror As MySqlException
  7.  
  8. MessageBox.Show("Cannot connect to database: " & myerror.Message)
  9.  
  10. Finally
  11.  
  12. MysqlConn.Dispose()
  13.  
  14. End Try
And This is all you code look like:
  1. Imports MySql.Data.MySqlClient
  2.  
  3. Public Class VBMYSQL
  4. Dim MysqlConn As New MySqlConnection
  5.  
  6.  
  7. Private Sub btnOpenConn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpenConn.Click
  8.  
  9. MysqlConn.ConnectionString = ("server=localhost;user id=root;password=;database=test")
  10.  
  11. Try
  12.  
  13. MysqlConn.Open()
  14.  
  15. MessageBox.Show("Connection to Database has been opened.")
  16.  
  17. Catch myerror As MySqlException
  18.  
  19. MessageBox.Show("Cannot connect to database: " & myerror.Message)
  20.  
  21. Finally
  22.  
  23. MysqlConn.Dispose()
  24.  
  25. End Try
  26.  
  27. End Sub
  28.  
  29. Private Sub btnCloseConn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCloseConn.Click
  30. Try
  31.  
  32. MysqlConn.Close()
  33.  
  34. MessageBox.Show("Connection to Database has been Closed.")
  35.  
  36. Catch myerror As MySqlException
  37.  
  38. MessageBox.Show("Cannot connect to database: " & myerror.Message)
  39. Finally
  40.  
  41. MysqlConn.Dispose()
  42. End Try
  43.  
  44. End Sub
  45. End Class
Then save the application and try it. Successful openning of Connection: Else if you out some error in database name or in user id:

Comments

Submitted byKathy (not verified)on Fri, 03/07/2014 - 21:28

Grabe kuya, sobrang thaaank you talaga! It has been a great help. God bless! :)
Submitted bySheila Mae (not verified)on Fri, 09/26/2014 - 00:46

Anong use po ba ng Dispose?
Submitted byjackorta (not verified)on Wed, 05/20/2015 - 14:18

thank you but how to connect to external database online
Submitted bylcmon Wed, 08/28/2019 - 07:59

This is very good and usefull project

Add new comment