Introduction
Using wizards in Visual Studio to connect to the MySQL database is a tricky business. Because MySQL is not in the VS2012 default supported database (such as SQL Server). In this article I'll explain how to use vb.net to connect to the MySQL database and execute the SQL statement (Select,update,delete), and I'll show you how to import the MySQL connector (connectors) into Visual Studio 2012.
Using code
You need to download the MySQL connector (mysql-connector-net) from http://dev.mysql.com/downloads/connector/and import it into Visual Studio. Unzip the downloaded file to a specified folder, such as C:\. Start Visual Studio.
To create a new project in VS2012, the project name can be whatever you like:
Open Project (Project)--> Add Reference (add Reference).
Select the DLL file you just downloaded and import it into your project.
Create a new VB class, named Itmysqldbas as follows:
Now you can directly use the code of the MySQL connection Class I wrote. I attached this class code here, or Mysqldb.zip.
You can copy the code of this class into your class, or simply put the class file into your project, depending on your decision. Whichever way you take it, you get a class that connects to the MySQL database and performs the Select, UPDATE, and delete operations.
I'll just use a few lines of code to add a simple GridView to my project and try to read the data from the database.
Copy Code code as follows:
Dim MyDB as New mysqldb
Protected Sub Page_Load (ByVal sender as Object, _
ByVal e as System.EventArgs) Handles Me.load
Try
Dim DataSet as New DataSet
Dim QueryResult as String = ""
DataSet = Mydb.executesql_dset ("Select COMMAND", QueryResult)
Gridview1.datasource = DataSet
Gridview1.databind ()
Catch ex as Exception
End Try
End Sub
When you execute the above code, the Executesql_dset function is called to fetch the data from the database. This function returns a dataset, and then you can play it in the project.
To perform an update or delete command, there is another method, as follows:
Copy Code code as follows:
Dim DataSet as New DataSet
Dim QueryResult as String = ""
Mydb.executedmlsql ("Update or delete SQL command", QueryResult)
If QueryResult = "SUCCESS" Then
' Your command is OK
Else
' Your command is not OK
End If
I hope this article will be useful to you.
MySQL is very small, very easy to download, and it is free (open source). I hope you can use the above class to connect to the MySQL database, and can execute (select,update,delete) command.
I'll explain some of the details of this class in another article to make it easier for you to modify the class yourself.