How To Connect To A MySql Database in VB.net

By: Syed M Hussain  

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: [email protected]
Author URL: www.cy2online.net


Archived Comments


Most Viewed Articles (in VB.net )

Latest Articles (in VB.net)