How To Connect To A MySql Database in VB.net

By: Syed M Hussain Emailed: 1700 times Printed: 2206 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 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

VB.net Home | All VB.net Tutorials | Latest VB.net 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

Comments(10)


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 vb.net 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 vb.net 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 VB.net
Simple Port Scanner application using Java

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

More Latest News
Most Viewed Articles (in VB.net )
How To Connect To A MySql Database in VB.net
How to export from DataGridView to excel using VB.net
XML and VB.net
Structured Exception Handling in VB.net
Nested If and Single line if statement
The For Loop in VB.net
Handling Strings in VB.net
Operators in VB.net
Sub Procedures and Functions in VB.net
Read Data from Excel using OLEDB in VB.NET 2005
Insert cell data in an Excel file using OLEDB in VB.net
How to export from database to excel using VB.net
Your first VB.NET Crystal Reports - A step by step guide
Server Socket Program sample in VB.net
Client Socket Program sample in VB.net
Most Emailed Articles (in VB.net)
Chat Server in VB.net
Procedure Delegates in VB.net
File operations in VB.net
Excel 2007 Data Validation Input Message using VB.net
Multi threaded Socket Programming in VB.net
XML and VB.net
How to create an XML file in VB.net
A tutorial on Chat Server and Chat Client in VB.net
Creating a Web Service in VB.net
“Using If with And” and Comparing two integers using If
Nested If and Single line if statement
The Select Case statement in VB.net
The For Loop in VB.net
What's New in VB .NET? A comparison of VB vs VB.net
What is .NET Framework and the Common Language Runtime?