Click here to Skip to main content
15,881,709 members
Articles / Programming Languages / Visual Basic
Tip/Trick

Connecting to MySQL databases using VB.NET

Rate me:
Please Sign up or sign in to vote.
4.91/5 (14 votes)
14 May 2013CPOL2 min read 290.4K   8.5K   24   10
How to connect to a MySQL database using VB.NET.

Introduction

Connecting to a MySQL database from Visual Studio 2012 using a wizard might be a bit tricky, because MySQL is not supported by default (like SQL Server). With this tip, I will show you how to connect to a MySQL database and run commands (select, update, delete) using VB.NET and I will also show you how to import MySQL connectors to Visual Studio 2012.

Using the Code

  1. You need to download the mysqlconnect (mysql-connector-net) from the MySQL website and add the DLL files to Visual Studio, the website is http://dev.mysql.com/downloads/connector/. Extract the download file, for example, in C:\ and go to --> Visual Studio -->
  2. Create a new project in VS2012 and name it whatever you like:

    Image 1

  3. Go to project --> Add reference.

    Image 2

    Image 3

  4. Select the DLL files from your folder and add them to your project.
  5. Create a new VB class, name it mysqldb as follows:

    Image 4

  6. Now you are ready to use the code of the class that I have created to connect to the MySQL database.  The code for this class is attached with this tip here, or from mySqlDB.zip.
  7. Now you can copy the methods from my class or add this class to your project directly, and then use the code in the project, it's up to you. In both cases, you will have a class that connects to a MySQL database and does select, update, delete operations.
  8. I will add a simple GridView to my project and try to get data from the database using very few lines of code.
    VB.NET
    Dim mydb As New mySqlDB
    
    Protected Sub Page_Load(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles Me.Load
        Try
            Dim dataset As New DataSet
            Dim queryresult As String = ""
            dataset = mydb.executeSQL_dset("SELECT COMMAND", queryresult)
            GridView1.DataSource = dataset
            GridView1.DataBind()
        Catch ex As Exception
    
        End Try
    End Sub 

    When you run the above code, you populate the data from the database in a dataset using the executeSQL_dset function. This function will return a dataset that you can then use anywhere in your project.

    To run an update or delete command, there is another method called:

    VB.NET
    Dim dataset As New DataSet
    Dim queryresult As String = ""
    mydb.executeDMLSQL("update or delete SQL command", queryresult)
    If queryresult = "SUCCESS" Then
        'your command is ok
    Else
        'your command is not ok
    End If

Hope this was helpful.

MySQL is very small and very easy to download, and it's free (open source). Hopefully with the above class, you can connect and run (select, update, delete) commands.

I shall try in another post to explain the class in detail in case you want to modify it and make changes.

License

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


Written By
Software Developer (Senior)
United States United States
Software Development using vb.net.
(web application)
This is a Collaborative Group (No members)


Comments and Discussions

 
Questionvb.net 2010 Pin
Member 1306192415-Mar-17 16:15
Member 1306192415-Mar-17 16:15 
QuestionUndefined items Pin
Member 125582854-Jun-16 4:02
Member 125582854-Jun-16 4:02 
GeneralWorked Fine Pin
Bawa90429-May-15 18:55
Bawa90429-May-15 18:55 
QuestionMySQL database not showing up in datasource dropdowns Pin
Member 115851586-Apr-15 9:59
Member 115851586-Apr-15 9:59 
Questionin vs 2012 alot of errors to start off maybe someone can guide us Pin
Member 1071122930-Mar-14 14:23
Member 1071122930-Mar-14 14:23 
Questionselect from mysql Pin
Lucian Vlaicu28-Nov-13 21:37
Lucian Vlaicu28-Nov-13 21:37 
AnswerRe: select from mysql Pin
Hassantga28-Nov-13 23:00
Hassantga28-Nov-13 23:00 
Questionproblem Pin
Member 1005595016-May-13 8:44
Member 1005595016-May-13 8:44 
Questionincase of modification/addition of tables/views to the mysql database then how to auto update the dataset of vb.net Pin
manipan9-Jan-13 23:06
manipan9-Jan-13 23:06 
AnswerRe: incase of modification/addition of tables/views to the mysql database then how to auto update the dataset of vb.net Pin
Biel Simon18-Feb-15 4:21
Biel Simon18-Feb-15 4:21 

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.