Free Information Technology Magazines and eBooks

Monday, June 22, 2009

How to connect to MySQL Database using VB.NET

In our company, our standard database platform is MS SQL Server. As VB.NET programmers, we all know how easy to connect to MS SQL Server through the use of System.Data.Sql namespace as part of .NET platform. But on our latest gate automation project, we are partnered with a 3rd party vendor where MySQL is their standard database platform of choice. Given the situation, we need to make our VB applications MySQL database friendly. For today's article, I will show you that it is as easy to connect to MySQL Database.


There are two method to connect to MySQL.
1. By using System.Data.OleDb Namespace
2. By using MySQL Connector for .NET

On this article, I will use the MySQL Connector for .NET. To begin our sample project, follow these step-by-step procedures.

1. Download and Install the latest MySQL Connector for .NET
2. Create your VB.NET project then add a reference to MySQL.Data by going to Project > Add Reference > .NET Tab.
3. On your imports directive, add the following namespaces


Imports System
Imports System.Data
Imports MySql.Data.MySqlClient


4. Now to connect MySQL database, copy and paste these codes.


Dim connString As String = "Database=TestData;Data Source=localhost;User Id=root;Password=mypassword"

Dim con As New MySqlConnection()

Try
con.ConnectionString = "Database=TestData;Data Source=localhost;User Id=root;Password=mypassword"
conn.Open()
Console.WriteLine("Connection Opened")

Catch ex As MySqlException
Console.WriteLine("Error: " & ex.ToString())
Finally
conn.Close()
Console.WriteLine("Connection Closed")
End Try


If you noticed, connecting to MySQL is similar to how you connect to a MS SQL Server database.

For more VB.NET coding tips & tricks, subscribe now.

6 comments:

Almond said...

Hello,

Good evening.

can i ask if this can be done in VB.Net using web forms.

I am trying so hard to look for it and i cant find it. I am just beginning to start learning .Net that is the reason i am asking this question.

I actually dont know if my question is valid or not...

I hope someone can clarify this matter to me.

Thanks in advance... =)

syed hassan said...

i use that code in Visual web Developer it is working good but plz change this

# Try
# con.ConnectionString = "Database=TestData;Data Source=localhost;User Id=root;Password=mypassword"
# conn.Open()
# Console.WriteLine("Connection Opened")
#
# Catch ex As MySqlException
# Console.WriteLine("Error: " & ex.ToString())
# Finally
# conn.Close()
# Console.WriteLine("Connection Closed")

in to

# Try
# con.ConnectionString = "Database=TestData;Data Source=localhost;User Id=root;Password=mypassword"
# con.Open()
# Console.WriteLine("Connection Opened")
#
# Catch ex As MySqlException
# Console.WriteLine("Error: " & ex.ToString())
# Finally
# con.Close()
# Console.WriteLine("Connection Closed")

Life is a hectic schedule........ said...

Can i know the code for fetching data from MySQL database and displaying the same in text boxes in the user interface?

Life is a hectic schedule........ said...

Can i know the code for fetching the data from MySQL database and displaying the same in the vb.net user interface?

Fryan Valdez said...

@Life is a hectic schedule

Its almost the same as selecting record in SQL database but replace:

SqlConnection with MySqlConnection, SqlCommand with MySqlCommand, and SqlDataReader with MySqlDataReader.

http://www.fryan0911.com/2009/06/vbnet-how-to-select-records-from-sql.html

Dave said...

Thanks, simple and it works.