Connect Visual Basic .NET to MySql Database

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This is a tutorial on how to connect visual basic .net to mysql database on an online server

First download MySql Connector/Net

Then add the following reference:


Sometimes MySql for somehow is not listed under .NET tab. Go to Browse tab, and navigate to the following path:

C:\Program Files (x86)\MySQL\MySQL Connector Net 6.6.4\Assemblies\v2.0
or C:\Program Files (x86)\MySQL\MySQL Connector Net 6.6.4\Assemblies\v4.0

and add MySql.Data.dll

Add the following code before Public Class Form1

Imports MySql.Data.MySqlClient

Then add the following declaration below Public Class Form1

Dim conn As New MySqlConnection

Then add the following Sub

Public Sub connect()
        Dim DatabaseName As String = "Database NAme"
        Dim server As String = "ip address here"
        Dim userName As String = "sarmasar here"
        Dim password As String = "password here"
        If Not conn Is Nothing Then conn.Close()
        conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)

        Catch ex As Exception
        End Try
    End Sub

Call the connect sub when the form first loads:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    End Sub

Insert into a mysql table:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Catch ex As Exception
        End Try
        Dim cmd As New MySqlCommand(String.Format("INSERT INTO `Products` (`upc` , `qty`) VALUES ('{0}' , '{1}')", "upc Value", "Quantity"))
    End Sub

Share This

Home | About | Contact | Privacy Policy

Copyright 2018 - All Rights Reserved.