C # Connection Operation MySQL DB instance (using official driver)

Source: Internet
Author: User
Tags odbc rollback mysql odbc driver

MySQL is very popular for its free and sufficient performance, of course, for small domestic companies, and even large companies if the copyright is thin, dare to take risks, just install a SQL Server, DB2, Oracle can be. For SQL Server database, because it and MS is a, naturally in the. NET class library has built-in support, if MySQL will have to find third-party driver-.net more talk about Provider. Here I am also as a pre-research topic, recorded in the C # connection MySQL two methods, respectively, using the MySQL official and SourceForge the last open source MySQL driver. As for the ODBC approach, do not mention, it is not very meaningful, the same to install a MySQL ODBC driver, and ODBC is so clumsy.

operate MySQL database using the MySQL official zone dynamic connection

Download driver, available in

Download, the current version is 6.3. You can choose a platform that can be either Microsoft Windows or. Net & Mono, the difference is that Microsoft Windows is an MSI file, which is a non-installed compressed package. But the MSI file was not successfully installed under my 64-bit XP, so I used the latter.

This connector net is not only a MySQL driver, but also provides support like Entity Frameword, ASP. Providers, Compact Framework, VS 2008, and also configures CSharp and V B's example. Please refer to the MySql.Data.chm file in the installation directory for detailed configuration and usage documentation.

Now, starting with the simplest example, the driver is installed, or the compression pack is unpacked, the Mysql.data.cf.dll, Mysql.data.entiry.dll, Mysql.visualstudio.dll, and Mysql.web.dll, we introduce mysql.data.dll in the project for the time being, using MySql.Data.MySqlClient; Introduce a namespace. Then the simple code is as follows:


The code is as follows:


Connection string

String connstr = "SERVER=LOCALHOST;DATABASE=UNMICC; UID=UNMICC; pwd=xxxxxx; Charset=utf8; ";

Mysqlconnection con = new mysqlconnection (CONNSTR);

Con. Open ();//Opening connection

Mysqlcommand cmd = new Mysqlcommand ("Select Now ()", con);

Object time = cmd. ExecuteScalar (); or CMD. ExecuteReader (); cmd. ExecuteNonQuery ();

MessageBox.Show (time. ToString ());

or Console.WriteLine (time. ToString ());

Con. Close ();

The connection string above should be understood and there are additional configuration parameters such as port number, connection pool-related configuration, etc., please refer to the Connection Options in the manual. The other operation is the standard ADO, and then can handle each step of the exception, catch MySql.Data.MySqlClient.MySqlException this type of exception. Other Mysqldataadapter and Mysqldatareader for populating the data are also ready.

I think when it comes to the formal application, the parametric query will definitely be raised to prevent SQL injection, so let's take a look at how this MySQL official driver handles parameterized queries. It is used in the same way as SQL Server Provider, which identifies parameters in the form of @author, and also supports AddWithValue (string name, object value). Please look at the code:


The code is as follows:


String sql = "Update wp_posts set [email protected] and [email protected] where [email protected]";

Can you use? Number of forms, such as, but? The form of the number is not recommended for use

String sql = "Update wp_posts set Post_author=?author and post_status=?status where Id=?id";

Mysqlcommand cmd = con. CreateCommand ();

Cmd.commandtext = SQL;

Cmd. Parameters.addwithvalue ("@author", 1);

Cmd. Parameters.addwithvalue ("Author", 1);

Cmd. Parameters.addwithvalue ("@status", "publish");

Cmd. Parameters.addwithvalue ("? status", "Publish");

Cmd. Parameters.addwithvalue ("@id", 23);

Cmd. Parameters.addwithvalue ("? id", 23);

Cmd. ExecuteNonQuery ();

Note that it is also possible to identify parameters in the form of author, but it is not recommended now, perhaps to unify into @author format. Do not know when can be like JDBC, direct use? To act as a placeholder.

For the InnoDB storage engine MySQL is supporting things, this official driver supports the code of things as follows:



Mysqltransaction trans = con. BeginTransaction (); Enable Things

Trans.commit (); Submit as normal

Trans. Rollback (); Rollback on exception



The next article will show you how to use the open source MySQL. Net driver on SourceForge to operate the MySQL database.

In addition to the Declaration, Running GuestArticles are original, reproduced please link to the form of the address of this article
C # Connection Operation MySQL DB instance (using official driver)

This address: http://www.paobuke.com/develop/c-develop/pbk23135.html






About the specific usage of async and await in C # String.Split usage in C # 90 minutes implementation of a programming language (Minimalist interpreter tutorial) C # Read and write Config file method
C # Operator Overloading usage Example analysis in C # 2 ways to call SAPI to implement speech synthesis C # interface (Interface) usage Analysis C # WinForm Control textbox input can only be a number method

C # Connection Operation MySQL DB instance (using official driver)

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.