SEARCH :

Custom Search

Tuesday, September 2, 2008

VB .NET to MySQL Database Connection

VB.NET is one of the most famous programming language. It's because .NET have a many feature and make programming more easy. In general, SQL Server is the database server that usualy used by user to store their data. But we can use another database that we can use. In this case, we will use MySQL. MySQL is one of database engine that used by many people in the world.

To make connection from VB.NET into MySQL we must do the following thing :
1. Download MySQL database engine from http://dev.mysql.com/downloads/

2. Install the MySQL

3. After that, you must download the connector from VB.NET into MySQL from www.mysql.com/products/connector/net

4. To use the connector, just Import the reference of MySQL DotNet Connector from Project->Add Reference

5. Make one module to do a connection into MySQL

Imports MySql.Data.MySqlClient

Module Connect
Public conn As New MySqlConnection

Public Sub ConnectDatabase()

Try
If conn.State = ConnectionState.Closed Then
conn.ConnectionString = "DATABASE=" & your database &";" _
& "SERVER=" & your host & ";user id=" & your username _
& ";password=" & your password & ";port=" & Your MySQL Port (3306) & ";charset=utf8"

conn.Open()
End If

Catch myerror As Exception
MsgBox("Connection Error")
End
End Try
End Sub

Public Sub DisconnectDatabase()
Try
conn.Close()
Catch myerror As MySql.Data.MySqlClient.MySqlException

End Try
End Sub
End Module


6. To connect to MySQL, just call the ConnectDatabase() from your form_load event


No comments: