How To Connect To A MySql Database in VB.net

By: Syed M Hussain Emailed: 1787 times Printed: 2623 times    

In this article I will explain how to connect to a MySql database in VB.net. 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 website.www.mysql.com.

Connector/Net
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 + \";\")
       Try
           con.Open()
           \'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


       Console.ReadKey()
   End Sub

End Module

Author Email: syed@cy2online.net
Author URL: www.cy2online.net

Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Actually, looking at the code, it looks as somethi
View Tutorial          By: Johnson at 2009-07-28 08:14:38

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

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

4. thanks .
it helps easy to connect..

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

5. Do you happen to have the code of if statement for
View Tutorial          By: James at 2012-06-04 09:02:31

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

7. I have a squid proxy installed as forward proxy an
View Tutorial          By: ruffy at 2012-12-20 12:34:15

8. Nice tutorial, just what I was looking for to get
View Tutorial          By: Melvin at 2013-01-10 01:00:06

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

10. i want to know how to connect to a remote mysql da
View Tutorial          By: lixiang at 2013-04-23 04:32:58

11. i am looking for the code which wiLL HAVE AFFECT I
View Tutorial          By: pooja at 2013-06-10 13:48:32

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

13. Some really wondrous work on behalf of the owner o
View Tutorial          By: how a push up bra works at 2017-06-02 15:06:08