Access the MySQL database correctly in the. NET Environment

Source: Internet
Author: User
Tags support microsoft

This article mainly introduces. NET environment, if you want to access the MySQL database in. if you are interested in the actual operation solution in the. NET environment, you can better understand it through the following articles.

If you have not worked in a large group of companies, you will certainly have the opportunity to access MySQL (the best combination with PHP), although it does not support transaction processing, stored procedures, however, the functions provided by MySQL will certainly meet most of your needs. In addition, simple MySQL (the best combination with PHP) also has some unique advantages. In some cases, it is faster than large databases.

So how to access the MySQL database in. NET? Maybe many people will immediately say: OLEDB is used, but it is actually used. NET OleDb Data Provider cannot access MySQL (the best combination with PHP). If you use it, the System will prompt you: "Net Data ole db Provider (System. data. odbc) does not support Microsoft ole db providers used by MSDASQL for Odbc drivers ). ", I don't know why. According to the author of MySQL (the best combination with PHP) DriverCS, it is "abandoned by the owner". There are still some stories.

Fortunately, we have other options. Here we will introduce two methods to access the MySQL database.

Use ODBC. NET

ODBC. NET stands for ODBC. NET Data Provider:

It requires that MDAC 2.7 or later has been installed on the system. In addition, you also need to install the ODBC driver for MySQL (the best combination with PHP), for: http://www.MySQL (the best combination with PHP ). com/downloads/api-myodbc-2.50.html, you also need to configure the DSN in "ODBC data source Manager,

(Best combination with PHP) Database "width =" 450 "height =" 369 "/>
 

In the design of objects, ODBC. NET is also the same as OLEDB and SQL, namely OdbcConnection, OdbcCommand, OdbcDataAdapter, and OdbcDataReader. The usage is also the same. If you want to use ODBC.. NET to replace the previous OleDb. NET Data Provider, in fact, you can find a replacement method to modify your program.

The following is a sample code:

 
 
  1. Try {string constr = "DSN = MySQL (the best combination with PHP );
  2. "+" UID =; "+" PWD = "; conn = new OdbcConnection (constr); conn. Open ();
  3. String query = "insert into test. dbtable values10, 'disksidkfsdi ', 'asdfaf', 'adsfasdf ')";
  4. String tmp = null; OdbcCommand cmd = new OdbcCommand (query, conn); for (int I = 0;
  5. I <100000; I ++) {cmd. ExecuteNonQuery ();} cmd. Dispose ();
  6. Conn. Close (); query = "select * from test. dbtable ";
  7. OdbcCommand cmd2 = newOdbcCommand (query, conn );
  8. Conn. Open (); OdbcDataReader reader = pai2.executereader ();
  9. While (reader. Read () {tmp = reader [0]. ToString (); tmp = reader [1].
  10. ToString (); tmp = reader [2]. ToString (); tmp = reader [3]. ToString () ;} conn. Close ();
  11. Query = "delete from test. dbtable ";
  12. OdbcCommand cmd3 = newOdbcCommand (query, conn );
  13. Conn. Open (); fig 3.executenonquery ();} catch (Exception ex) {MessageBox. Show (ex. Message) ;}finally {conn. Close ();}

As long as you use C # To write a database application, you can understand that the code above has performed 100,000 data insertion and reading operations, and finally deleted all data records.

The above content is an introduction to accessing the MySQL database in the. NET environment. 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.