How to Connect MySQL Database to VB.Net

0
334

This Tutorial is all about How to Connect MySQL Database to VB.Net. In this tutorial you will learn about How to Connect MySQL Database to VB.Net So lets get Started:

  • First is open the Visual Basic, Select File on the menu, then click New and create a new project.

  • Then a New Project Dialog will appear. You can rename your project, depending on what you like to name it. After that click OK

  • Design your form like this just like what I’ve shown you below.
    Be sure to add MySql.Data.dll.
    You should have an Existing Database to surely connect.
    Add a Button to the Form.
  • Add this code before the Public Class Form1.
    1. Imports MySql.Data.MySqlClient
  • Add this code below the Public Class Form1.
    1. Dim conn As New MySqlConnection
  • Add this Sub.
    1. Public Sub connect()
    2. Dim DatabaseName As String = "sample1"
    3. Dim server As String = "localhost"
    4. Dim userName As String = "root"
    5. Dim password As String = ""
    6. If Not conn Is Nothing Then conn.Close()
    7. conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
    8. Try
    9. conn.Open()MsgBox("Connected")
    10. Catch ex As Exception
    11. MsgBox(ex.Message)
    12. End Try
    13. conn.Close()
    14. End Sub
  • Add this code to the Button.
    1. connect()
  • Finally Click F5 to run Program.

Output:

If you have any comments or suggestion about on How to Connect MySQL Database to VB.Net, Please Feel Free to contact our webpage.

Download How to Connect MySQL Database to VB.Net Code Here

Other articles you might read also:

 

Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here