How To Connect To A MySql Database in

By: Syed M Hussain Emailed: 1757 times Printed: 2448 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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:
Author URL: Home | All Tutorials | Latest Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.

Bookmark and Share


1. View Comment

Actually, looking at the code, it looks as something that will work fine based on the experience i have got with dotNet programming. i will install the Connector/Net to see how this work. Thanks.

View Tutorial          By: Johnson at 2009-07-28 08:14:38
2. View Comment

thanx a lot.............!!!! u r best..........

View Tutorial          By: Pratik at 2010-04-12 12:41:30
3. View Comment

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
4. View Comment

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
5. View Comment

thanks - this really helped!!

View Tutorial          By: mike at 2012-06-14 19:45:17
6. View Comment

I have a squid proxy installed as forward proxy and can not connect to the server even after enabling port 3306.

Is there any other way of connecting Mysql database (like Direct Connect) over internet .. I mean other than ODBC and MySqlConnector for .Net programmers?

View Tutorial          By: ruffy at 2012-12-20 12:34:15
7. View Comment

how to connect database simple method

View Tutorial          By: kavimozhian at 2013-04-08 11:14:29
8. View Comment

i want to know how to connect to a remote mysql database which in the interenet server. i use language. if you know ,pleace send email to me, thanks very much!

View Tutorial          By: lixiang at 2013-04-23 04:32:58
9. View Comment

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
10. View Comment

give the picture view of connecting with mysql

View Tutorial          By: Murugamani at 2014-09-09 06:12:58

Your name (required):

Your email(required, will not be shown to the public):

Your sites URL (optional):

Your comments:

More Tutorials by Syed M Hussain
How To Connect To A MySql Database in
Simple Port Scanner application using Java

More Tutorials in
Scope in
Unstructured Exception Handling in
Structured Exception Handling in
Creating Sub Procedures in
Creating Functions in
Passing a Variable Number of Arguments to Procedures in
Specifying Optional Arguments with default values in Procedures in
Preserving a Variable's Values between Procedure Calls in
Procedure Delegates in
Properties in
Understanding Scope in
Using Resume Next and Resume Line in
Using On Error GoTo 0 in
Getting an Exception's Number and Description in
Raising an Exception Intentionally in

More Latest News
Most Viewed Articles (in )
How to export from DataGridView to excel using
Your first VB.NET Crystal Reports - A step by step guide
How to export from database to excel using
Insert cell data in an Excel file using OLEDB in
The Option and Imports Statements in VB .NET
Handling Dates and Times in
Sub Procedures and Functions in
A tutorial on Chat Server and Chat Client in
To read the entire worksheet in an Excel workbook through Code
How To Connect To A MySql Database in
What's New in VB .NET? A comparison of VB vs
Arrays and Dynamic Arrays in
How to Open and Edit Cells in an Excel 2007 file in VB.NET
Using Resume Next and Resume Line in
For Loop in
Most Emailed Articles (in
How to export from DataGridView to excel using
How to send email using VB.NET code
Data types in
Arrays and Dynamic Arrays in
If…Else Statements in
ArrayList data structure in
Name Value Collecion in
Implicit & explicit type Conversion in
String equals, Copy to & Copy to in
File stream operations in binary reader and writer
Insert cell data in an Excel file using OLEDB in
Update cell data in an Excel file using OLEDB in
How to find IP Address of Host using .net code
Specifying Optional Arguments with default values in Procedures in