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:
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... =)
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")
Can i know the code for fetching data from MySQL database and displaying the same in text boxes in the user interface?
Can i know the code for fetching the data from MySQL database and displaying the same in the vb.net user interface?
@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
Thanks, simple and it works.
Post a Comment