Click here to Skip to main content
15,038,345 members
Articles / Programming Languages / Visual Basic
Posted 26 Aug 2014

Tagged as


1 bookmarked

MySQL Connection In Windows Application Using VB.NET

Rate me:
Please Sign up or sign in to vote.
4.18/5 (3 votes)
26 Aug 2014CPOL
How to use MySQL as database in VB.NET applications.


Nowadays, many people are trying to use MySQL as database because MySQL is an Open-Source Relational Database Management System which is mainly widely used in Web Applications. Here, we will go through Windows based application as an example for MySQL connection.

Using the Code

Before making any connection, we need to download the latest version of MySQL Connector from the official website of MySQL: after downloading DLL files from the official site.

Step 1

Now, we have to place DLL files in our project as Reference, So to place Reference, right click on project, go through Add reference, then Select Browser button and select DLL files from where DLL files are saved directory. This Connection String has been placed in App.config file. Then, in Connecting string for DataSource as follows:

add name="Demo" connectionString="server=localhost;database=demo;uid=root;password=;" 

Step 2

Now, we have to import Imports files for MySQL Client as they are as follows:

Imports MySql.Data
Imports MySql.Data.MySqlClient

Here, I will use Login for example to make connection between database and application, go through code as follows:

Public Class Login
    Public conn As SqlConnection

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim a, b As String
        a = TextBox1.Text
        b = TextBox2.Text
        Dim flag As Integer = 0
            conn = New MySqlConnection(ConfigurationManager.ConnectionStrings("Demo").ConnectionString)
            '' MySQL Commands
            Dim mySelectQuery As String = "select * from demo.login where username='" & TextBox1.Text & "' and u_password='" & TextBox2.Text & "' "
            Dim myCommand As New MySqlCommand(mySelectQuery, conn)

            Dim rd As MySqlDataReader
            rd = myCommand.ExecuteReader()

        '' Data Reading
            While (rd.Read())
            If (a = rd(0).ToString.Trim() And b = rd(1).ToString.Trim()) Then
                flag = 1
                Exit While
                flag = 0
            End If
        End While
        If (flag = 1) Then
            MsgBox("Login Sucessfully", MsgBoxStyle.OkOnly, "Done")
            MsgBox("Username or Password is Wrong....", MsgBoxStyle.Critical,
        End If
            '' Closing Database.
        Catch ex As Exception
            MsgBox("Error Loading Database", MsgBoxStyle.Critical, "Error")
        End Try
    End Sub

For Insert and Update as follows:

conn = New MySqlConnection(ConfigurationManager.ConnectionStrings("Demo").ConnectionString)
            Dim query as String
            query = "update demo.login set <set as="" follows="" query="">
Catch ex as Exception
MsgBox("Some message as example shown above")
End Try

Dim rd As SqlDataReader
conn = New MySqlConnection(ConfigurationManager.ConnectionStrings("Demo").ConnectionString)
            Dim query as String
            query = "insert into demo.login values <insert>
            Dim Command As New MySqlCommand(query, con)
            Dim rd As MySqlDataReader
            rd = Command.ExecuteReader()
            MsgBox("Data saved Successfully", MsgBoxStyle.MsgBoxRight, "Done")
            Catch ex as Exception
                MsgBox("Some message goes here")
            End Try

This topic helps you to create a connection to MySql Database with Select, Update and Insert queries.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
India India
No Biography provided

Comments and Discussions

QuestionAdd References Pin
evry1falls7-Jul-20 8:15
Memberevry1falls7-Jul-20 8:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.