Explains how to join MySQL database with ASP. NET

Source: Internet
Author: User
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 (. NETFramework) to expand the scope of use of MySQL. Let's learn how to use MySQL in. Net applications. MySQL community has developed My

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.

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 (formerly 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 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:

Using MySql. Data. MySqlClient;

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

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 and connections used to fill the DataSet object and update 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.

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.

A connection string tells the code to use the specified user name and password to connect to a MySQL server named MySQLTestServer and access the techrepublic database. On my test machine, I set to allow anonymous login (this setting has a very large security vulnerability, so we do not recommend that you do this on the production server ), therefore, the following connection string will be used in the example:

"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 the connection is established, 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, then runs a show databases command on the server, and then displays the result through the MySqlReader object.

Sending the show databases command to the MySQL server using the MySqlCommand object is the same as inputting this 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 results (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:

Using ByteFX. Data. MySqlClient;

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.

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.