The correct solution for connecting ASP. NET to MySQL database

Source: Internet
Author: User

The following articles mainly describe ASP. NET to connect to the MySQL database. If you encounter ASP. NET connection to the MySQL database, but you do not know how to solve it correctly, then the following article must be a mentor for you.

Because of its competitive prices and ease of use, MySQL has gradually increased its market share. The open source code community has developed a database connector that can be used in. Net Framework (. NET Framework) to extend the scope of use of MySQL. Let's learn how to use MySQL in. Net applications.

MySQL gradually becomes a practical database solution when selecting a database platform. What proves this is that many companies choose mySQL as their database platform, such as Google, The Associated Press, and NASA ). Although for an open source code, the low price is often used as the main advantage to persuade customers, but for a large-scale organization like Google, they do not trust in storing very useful information in a database product with only price advantages. The real strength of MySQL far exceeds his price advantage. It provides a wide range of additional tools from the open source code community and commercialization.

Data integration with. NET

The MySQL Community has developed MySQL Data interfaces, which provide basic functions for connecting data sources and program code. On Windows, the following MySQL connector is available:

MySQL Connector/Net 1.0 is called ByteFX. Data): It is an open source code. NET Data interface designed for MySQL. It is fully developed using C #. We can find it on MySQL.com. Note: In this example, we will use the MySQL Connector/Net 1.0 data interface to connect to the MySQL database. you can install it easily by using the Windows installer, its code examples and documents are also included .)

MySQLDirect. NET Data Provider: A commercial Data interface developed by Core Lab. The price is determined by the type of the purchased license, but we can download its trial version.

If you use Mono, you can find the PHP connector download on the Mono website. If you run Mono on Windows, the connector you downloaded contains the installer. If not, download the appropriate connector based on your operating system type.

Use the MySQL Data Interface

After installing the MySQL data interface, you must introduce it in your code to use it. You can use the namespace MySql. Data. MySqlClient to connect to the MySQL server. In C #, you can use the using statement to introduce the MySQL data interface:

 
 
  1. using MySql.Data.MySqlClient; 

In addition, you can also use the Import command in an ASP. NET Web Form to introduce the MySQL data interface.

 
 
  1. <%@ Import Namespace "MySql.Data.MySqlClient" %> 

Alternatively, you can use this namespace in your code to write the complete path of all classes, however, this will waste more bytes than using the Import command to Import more characters. After the namespace is specified, we can interact with the MySQL database. MySql. Data. MySqlClient this namespace provides many classes for processing MySQL Data. Below is a sample of these classes:

MySqlConnection: Manages connections to MySQL servers/databases;

MySqlDataAdapter: a set of commands used to fill the DataSet object and update the MySQL database and to connect to the MySQL database;

MySqlDataReader: allows you to read data from a MySQL database. It is a one-way data stream;

MySqlCommand: Provides the function of sending commands to the database server;

MySqlException: Provides exception handling when a problem occurs.

We will use some of these classes to interact with our sample database.

Connect to the MySQL database

The first step to use the MySQL database is to establish a connection with the database through the MySQLConnection class. Using a connection string, MySqlConnection is instantiated as an example. The connection string tells the Code where to find the MySQL server and other options.

Table A is an example of A connection string. The connection string tells the code to connect to a MySQL server named MySQLTestServer with the specified user name and password and access the techrepublic database. I set a security vulnerability on my test machine to allow anonymous login. Therefore, we do not recommend that you do this on the production server ), therefore, the following connection string will be used in the example:

 
 
  1. "server=localhost; database=sitepoint;" 

After the connection string is specified, the Open method of the MySqlConnection object is called and the connection is opened. After connecting to the MySQL database, you can send commands to the MySQL database or obtain data from the database.

Combination of ASP. NET and MySQL

Let's further discuss how to combine the MySqlConnection class and other classes to generate a database list on a MySQL server. Table B lists a web form written in C # using ASP. NET. It establishes a connection, and then runs the show databases command to the server. Then, the result is displayed through the MySqlReader object.

Using the MySqlCommand object to send the show databases command to the MySQL server is the same as entering the command directly in the MySQL management tool. The only difference is that we must use another object in the code to obtain the result set. The MySqlDataReader object is instantiated when obtaining the result through the ExecuteReader method of the MySqlCommand class ). The GetString method of the MySqlDataReader object is used to display data in the result set through ASP. NET label control. The pointer 0 of the GetString method specifies the first column of data that shows the current row of the result set in the while loop.

Mono prompt

If you use the open-source Mono development platform, the code in the example can run normally with only a few changes. The Data interface of MySQL is in the space name ByteFX. Data. MySqlClient, rather than the MySql. Data. MySqlClient space name on Windows. In fact, MySQL's data interface was originally developed by ByteFX, but was subsequently acquired by MySQL. Therefore, if you use Mono, you must declare the space name as follows:

 
 
  1. using ByteFX.Data.MySqlClient; 

Conclusion

The combination of MySQL and. NET provides a powerful development platform. MySQL has received powerful technical support in the open-source community, and. NET is also accepted by the open-source community through Mono. This combination provides a highly flexible development platform in Windows and other languages such as UNIX or Linux. The above content is an introduction to connecting ASP. NET to the MySQL database. I hope you will have some gains.

Related Article

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.