26 | 02 | 2017

Connect to MySQL using vb.net

To connect to a MySQL database using VB.net take the following steps. 

1. Set the reference to MySQL.Data

2. Add the following code to VB.net

3. Change the server details and connect to the database.

Imports MySql.Data.MySqlClient

Dim ConnSQL As MySqlConnection
Dim data As DataTable
Dim da As MySqlDataAdapter
Dim cb As MySqlCommandBuilder

Function ConnectToDB() As Boolean

'*** Code from VisibleVisual.com ********

If Not ConnSQL Is Nothing Then ConnSQL.Close() 'Close a connection if open

Dim connStr As String
Dim MySQLServer As String = "NAMEOFSERVER" 'Type the name of the MYSQL server here.
Dim MySQLloginID As String = "LOGIN ID" 'Type the login name for the server here
Dim MySQLPassword As String = "PASSWORD" 'Type the password to the server here
Dim MySQLdatabasename As String = "DATBASE" 'Type the mysql database name here

connStr = String.Format("server=" & MySQLServer & ";" _
& "user id=" & MySQLloginID & ";" _
& "database=eurosort_part" & MySQLdatabasename & ";")

ConnSQL = New MySqlConnection(connStr)
ConnectToDB = True
Catch ex As MySqlException
ConnectToDB = False
MessageBox.Show("Error connecting to the server: " + ex.Message)
End Try

End Function

Sign up now and upload your code to the website.

Help us to continue.....
Articles View Hits
Latest Articles