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:
- Try {string constr = "DSN = MySQL (the best combination with PHP );
- "+" UID =; "+" PWD = "; conn = new OdbcConnection (constr); conn. Open ();
- String query = "insert into test. dbtable values10, 'disksidkfsdi ', 'asdfaf', 'adsfasdf ')";
- String tmp = null; OdbcCommand cmd = new OdbcCommand (query, conn); for (int I = 0;
- I <100000; I ++) {cmd. ExecuteNonQuery ();} cmd. Dispose ();
- Conn. Close (); query = "select * from test. dbtable ";
- OdbcCommand cmd2 = newOdbcCommand (query, conn );
- Conn. Open (); OdbcDataReader reader = pai2.executereader ();
- While (reader. Read () {tmp = reader [0]. ToString (); tmp = reader [1].
- ToString (); tmp = reader [2]. ToString (); tmp = reader [3]. ToString () ;} conn. Close ();
- Query = "delete from test. dbtable ";
- OdbcCommand cmd3 = newOdbcCommand (query, conn );
- 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.