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: Step1: Create a Database.
  1. CREATE DATABASE `test` ;
Step2: Open the Visual Basic 2008, create a Project, and set the Form just like this. First Form Step3: 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 Step4: 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"
  5.  
  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. Step5: 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
  5.  
  6. If Button1.Text = "connect" Then 'checking the text of a button
  7.  
  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

Add new comment