How To Connect To A MySql Database in

By: Syed M Hussain  

In this article I will explain how to connect to a MySql database in MySql is an open source free database. In order to follow along with this tutorial you will need to have MySql installed on your machine. You can download MySql from the MySql

As well as downloading the MySql database, you will need to download the MySql Connector/Net driver. This driver enables developers to easily create .NET applications. Developers can build applications using their choice of .NET languages. MySql Connector/Net is a fully-managed ADO.NET driver written in 100% pure C#.

You will need to download this driver from the MySql website. Once downloaded simply go through the installation process.

Console Application
It’s time to look at some code. Load the Visual Studios IDE and select a new Visual Basic Console Application. Once your new projected has loaded you should have an empty module.

The first thing we need to do is add a reference to the MySql assembly. Click the Project from the menu and then select Add Reference. Under the .Net tab, browse for the MySQL.Data assembly.

Now that the reference has been added, we need to use the Imports directive to import the
MySql.Data.MySqlClient namespace.

Your imports directives should look like the following:

Imports System.Data
Imports MySql.Data.MySqlClient

To connect to the MySql database, we need to use the MySqlConnection Class. This class has two constructors. The default constructor takes no arguments. The second constructor takes a connection string as an argument. If you use the default constructor, you can specify the connection string later in your code by using the ConnectionString property.

Below in listing 1.1 we use the second constructor.

Listing 1.1
con = New MySqlConnection(\"Server=\" + _host + \";User Id=\" + _user + \";Password=\" + _pass + \";\")
In listing 1.1 a MySqlConnection object is created. This object is then used to connect to the database.

Listing 1.2 below shows the complete code to connect to a MySql database and query a table. The MySql database is used for this example.

Listing 1.2
Imports System.Data
Imports MySql.Data.MySqlClient

Module Module1
   Private con As New MySqlConnection
   Private cmd As New MySqlCommand
   Private reader As MySqlDataReader

   Private _host As String = \"localhost\" \' Connect to localhost database
   Private _user As String = \"root\" \'Enter your username, default is root
   Private _pass As String = \"\" \'Enter your password

   Sub Main()
       con = New MySqlConnection(\"Server=\" + _host + \";User Id=\" + _user + \";Password=\" + _pass + \";\")
           \'Check if the connection is open
           If con.State = ConnectionState.Open Then
               con.ChangeDatabase(\"MYSQL\") \'Use the MYSQL database for this example
               Console.WriteLine(\"Connection Open\")
               Dim Sql As String = \"SELECT * FROM USER\" \' Query the USER table to get user information
               cmd = New MySqlCommand(Sql, con)
               reader = cmd.ExecuteReader()

               \'Loop through all the users
               While reader.Read()
                   Console.WriteLine(\"HOST: \" & reader.GetString(0)) \'Get the host
                   Console.WriteLine(\"USER: \" & reader.GetString(1)) \'Get the username
                   Console.WriteLine(\"PASS: \" & reader.GetString(2)) \'Get the password
               End While

           End If

       Catch ex As Exception
           Console.WriteLine(ex.Message) \' Display any errors.
       End Try

   End Sub

End Module

Author Email: [email protected]
Author URL:

Archived Comments

1. Some really wondrous work on behalf of the owner of this website, utterly outstanding content materi
View Tutorial          By: how a push up bra works at 2017-06-02 15:06:08

2. give the picture view of connecting with mysql
View Tutorial          By: Murugamani at 2014-09-09 06:12:58

3. i am looking for the code which wiLL HAVE AFFECT IN DATABASE CAN I GET SOMETHING LIKE IT.
View Tutorial          By: pooja at 2013-06-10 13:48:32

4. i want to know how to connect to a remote mysql database which in the interenet server. i use
View Tutorial          By: lixiang at 2013-04-23 04:32:58

5. how to connect database simple method
View Tutorial          By: kavimozhian at 2013-04-08 11:14:29

6. Nice tutorial, just what I was looking for to get me started. The only confusing part with use of e
View Tutorial          By: Melvin at 2013-01-10 01:00:06

7. I have a squid proxy installed as forward proxy and can not connect to the server even after enablin
View Tutorial          By: ruffy at 2012-12-20 12:34:15

8. thanks - this really helped!!
View Tutorial          By: mike at 2012-06-14 19:45:17

9. Do you happen to have the code of if statement for only two users with no database??????????????????
View Tutorial          By: James at 2012-06-04 09:02:31

10. thanks .
it helps easy to connect..
please give code for storing data to MySQL

View Tutorial          By: bastin at 2011-12-09 19:49:34

11. Thats should work. i'l try it. Thnx!
View Tutorial          By: s2me at 2010-12-18 08:22:32

12. thanx a lot.............!!!! u r best..........
View Tutorial          By: Pratik at 2010-04-12 12:41:30

13. Actually, looking at the code, it looks as something that will work fine based on the experience i
View Tutorial          By: Johnson at 2009-07-28 08:14:38

Most Viewed Articles (in )

Latest Articles (in

Comment on this tutorial