Using MySQL in the VB. NET application _ MySQL

Source: Internet
Author: User
This article mainly introduces how to use MySQL in VB. NET applications. The operation is based on Visual Studio IDE. if you need it, refer Xuyan

Using the wizard to connect to the MySQL database in Visual Studio is quite tricky. Because MySQL is not supported by VS2012 by default (such as SQL Server ). This article describes how to use VB.. NET connects to the MySQL database and executes SQL statements (SELECT, UPDATE, DELETE). at the same time, I will show how to import the MySQL Connector (connectors) to Visual Studio 2012.

Use code

You need to download the mysqlconnector (mysql-connector-net) from http://dev.mysql.com/downloads/connector/and import it to Visual Studio. Decompress the downloaded file to a specified folder, such as C :\. Start Visual Studio.
Create a new project in VS2012. the project name can be whatever you like:

Open project --> Add reference ).

Select the downloaded DLL file and import it to your project.
Create a new VB class named itmysqldbas as follows:

Now you can directly use the MySQL connection class code I wrote. I included the code of this class here, namely mysqldb.zip.
You can copy the code of this class to your class, or simply put this class file directly into your project, depending on your decision. No matter which method you use, you can get a class that can connect to the MySQL database and perform SELECT, UPDATE, and DELETE operations.
I will just use a few lines of code to add a simple GridView to my project and try to read data from the database.

The code is 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.exe cuteSQL_dset ("select command", queryresult)
GridView1.DataSource = dataset
GridView1.DataBind ()
Catch ex As Exception

End Try
End Sub

When you execute the above code, you will call the executeSQL_dset function to retrieve data from the database. This function returns a dataset, and then you can use it in the project.

To execute the update or delete command, you can use the following method:

The code is as follows:

Dim dataset As New DataSet
Dim queryresult As String = ""
Mydb.exe cuteDMLSQL ("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 and easy to download, and it is free (open source ). We hope that you can use the above class to connect to the MySQL database and execute the (SELECT, UPDATE, DELETE) command.

I will explain some details about this class in another article, so that you can modify it yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.