MySQL is more powerful with asp.net

Source: Internet
Author: User
Tags exception handling extend interface connect mysql mysql database visual studio advantage
Asp.net|mysql

The open source community has developed to extend the scope of MySQL usage. NET Framework (. NET Framework) is a database connector that can be used in the. Let's learn how to do it in. NET applications, using MySQL.
Due to competitive prices and ease of use, MySQL is gradually upgrading its market share. The open source community has developed to extend the scope of MySQL usage. NET Framework (. NET Framework) is a database connector that can be used in the. Let's learn how to do it in. NET applications, using MySQL.

TechRepublic's. NET Newsletter, published every Wednesday, contains network services, ASP.net, ado.net, and Visual Studio. NET-related practical tips and code examples. Subscribe now automatically!

MySQL is becoming a practical database scheme when choosing the database platform. To prove this, many companies have opted for MySQL as their database platform, such as Google, the Associated Press, and NASA, the United States National Aeronautics and Space Agency (associated). While low prices are often used as a major advantage for an open-source source, they will not be reassuring to store very useful information in a database product with a price advantage for large organizations like Google. MySQL's real strength far exceeds his price advantage, and he offers a wealth of additional tools from the open source community and commercialization.

Data consolidation with. Net

The MySQL community has developed a MySQL data interface that provides basic functionality for connecting data sources and program code. On the Windows platform, like the following MySQL connector:

* MySQL Connector/net 1.0 (previously known as Bytefx.data): is an open source. Net Data interface designed for MySQL. It was developed entirely in C # and we can find it on the mysql.com website. (Note: In the example of this article, we use the MySQL connector/net 1.0 data interface to connect to the database, which can be easily installed with Windows Installer, with code instances and documents included.) )

* mysqldirect. NET data Provider: A commercial data interface developed by Core Lab. His price is determined by the type of license purchased, but we can download its trial version.

If you use Mono, you can find the download of the PHP connector on the Mono website. If you run mono on a Windows platform, the connectors you download include the installer. If not, then download the appropriate connector based on the type of operating system you are using.

Using the MySQL Data interface

Once you have installed the MySQL data interface, you must introduce it into your code to use it. You can use the MySql.Data.MySqlClient namespace to connect to the MYSQL server. In C #, you can use a using statement to introduce the MySQL data interface:

Using MySql.Data.MySqlClient;

Alternatively, you can introduce the MySQL data interface in a asp.net Web Form (Web Form) by using Import directives:

  

Alternatively, you can write the full path of a class in your code while using the namespace, but that would waste more bytes than importing more characters using the import directive. Once we have specified a name space, we can interact with the MySQL database for data. MySql.Data.MySqlClient This namespace provides a number of classes for handling MySQL data. Here is a sample of these classes:

* Mysqlconnection: Manage the connection with MySQL server/database;

* Mysqldataadapter: A set of commands and connections for populating dataset objects and updating MySQL databases;

* Mysqldatareader: Allows you to read data from a MySQL database. It is a one-way flow of data;

* Mysqlcommand: Provides the function of sending instructions 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.

Connecting to the MySQL database

The first step in using the MySQL database is to establish a connection through the Mysqlconnection class and the database. With a connection string, Mysqlconnection will be instantiated as an example. The connection string will tell the code where to find the MySQL server and some other options.

A connection string tells the code to use the specified username and password to connect to a MySQL server named Mysqltestserver and enter the TechRepublic database. I set up an anonymous login on my test machine (which has a very large security vulnerability, so it is not recommended that you do so on the production server), so the following connection string will be used in the example:

"Server=localhost; Database=sitepoint; "

When a connection string is specified, the Mysqlconnection object's Open method is invoked and the connection is opened. Once the connection is established, you can send commands to the MySQL database or get the data from the database.

Asp. Combination of net and MySQL

Let's take a closer look at the combination of the Mysqlconnection class and other classes to generate a list of databases on a MySQL server. Table B lists a asp.net Web form that is written using C #. It establishes a connection, then gives the server an instruction (show DATABASES), and then displays the results through the Mysqlreader object.

Using the Mysqlcommand object to send the show DATABASES command to the MySQL server and enter the command directly into the MySQL administration tool The result is the same. The only difference is that we have to use another object in our code to get the result set. The Mysqldatareader object is instantiated when it obtains the result (through the ExecuteReader method of the Mysqlcommand class). The GetString method of the Mysqldatareader object is used to display the data in the result set through ASP.net label control. Pointer 0 of the GetString method specifies the first column of data that displays the current row of the result set (in the while loop).

Mono Tips

If you use an open source mono development platform, the code in the example needs only a small change to run normally. MySQL's data interface is ByteFX.Data.MySqlClient this space name, not the MySql.Data.MySqlClient space name on Windows. In fact, the MySQL data interface was originally developed by Bytefx Company, but was acquired by MySQL company. So if you use mono, you have to declare space names like this:

Using ByteFX.Data.MySqlClient;

Conclusion

The combination of MySQL and. NET provides a powerful development platform. MySQL has strong technical support in the open source community. NET is also accepted by the open source community through Mono. Such a 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.