Skip to main content

Connecting MySQL Database and Visual Basic 2008 Using a Class

In this tutorial I will teach you how to use a class in connecting the MySQL Database and Visual Basic 2008. What is a class? A Class is a group of object that defines its functionality as a set of properties and the methods that will respond to. And it is also a container of your data and code. Wherein you can limit the data that you’re accessing. In Object-Oriented Programming(OOP) , classes, methods and properties help you create a complete application.

Let's begin:

Create a Database.

  1. CREATE DATABASE `test` ;


Open the Visual Basic 2008, create a Project, and set the Form just like this.

First Form


Go to the solution explorer, right click the name of your project, click the add and add the class and name it “connection”.

Solution Explorer


Inside the class(connection), do the following codes for the connection of MySQL Database.

  1. Imports MySql.Data.MySqlClient
  2. Public Class connection
  3. 'set a string connection
  4. Private strCon As String = "server=localhost;user id=root;database=test"
  6. 'set a connection
  7. Private Function Mycon() As MySqlConnection
  8. Return New MySqlConnection(strCon)
  9. End Function
  10. 'pass the value of Mycon to Connection
  11. Private Connection As MySqlConnection = Mycon()
  12. 'set a property of connection
  13. Property con() As MySqlConnection
  14. Get
  15. 'return the connection
  16. Return Connection
  17. End Get
  18. Set(ByVal value As MySqlConnection)
  19. 'set the connection
  20. Connection = value
  21. End Set
  22. End Property
  23. End Class

Discriptions: Property has function that can be read-only,write-only and read-write. GET accessor provides to read the access and the Set accessor provides to write the access.


Go back to the design views, double click the button and do the following codes.

  1. Public Class Form1
  2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. 'call a class and set a new class of connection
  4. Dim Mycon As connection = New connection
  6. If Button1.Text = "connect" Then 'checking the text of a button
  8. 'openning the connection
  9. Mycon.con.Open()
  10. If Mycon.con.State = ConnectionState.Open Then 'check if the connection is open or not.
  11. 'result
  12. Button1.Text = "disconnect" 'change the text of the button
  13. Label1.Text = "Connected" 'change the text of the label
  14. Else
  15. Button1.Text = "connect" 'change the text of the button
  16. Label1.Text = "Disconnected"
  17. End If
  18. Else
  19. 'clossing the connection
  20. Mycon.con.Close()
  21. If Mycon.con.State = ConnectionState.Open Then
  22. Button1.Text = "disconnect" 'change the text of the button
  23. Label1.Text = "Connected" 'change the text of the label
  24. Else
  25. Button1.Text = "connect" 'change the text of the button
  26. Label1.Text = "Disconnected" 'change the text of the label
  27. End If
  28. End If
  29. End Sub
  30. End Class

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.