. Net connection MySQL database driver package MySQL. Data. dll
It encapsulates the ADO. Net operations of all MySQL Databases in almost the same way as SQL Server (for example, sqlcommand in SQL Server is called mysqlcommand in MySQL ).
Use the following method:
1) install the database driver package MySQL. Data. dll (mysql-connector-net-5.0.8.1.exe ).
2) introduce the mysql. Data. dll package in your own solution (project. The steps are as follows:
A) Right-click the reference in solution.
B) on the displayed page, click Add reference.
C) Click browsing options in the pop-up dialog box.
D) Find the mysql. Data. dll package you just installed in the search range text box.
Note: The default path of MySQL. Data. dll is:
C: \ Program Files \ mysql connector net 5.0.8.1 \ binaries \. NET 2.0
3) Write a connection to the MySQL databaseCode(Example :)
Using mysql. Data. mysqlclient; <--- this space must be introduced
// The server address of the data source database
// Database Name of the database to be connected
// User ID: User Name of the database
// Password Database Password
// Port Database port number (3306 can be omitted)
// Connect to the database
Try
{
Myconn = new mysqlconnection ("Data Source = 192.168.1.100;" + "Initial catalog = dbname;" + "User ID = root; Pwd = root ");
Myconn. open ();
MessageBox. Show ("database connected ");
}
Catch (mysqlexception myerror)
{
MessageBox. Show ("MySQL connection error:" + myerror. Message );
}
// Query the database
Mysqlcommand com;
Mysqldatareader Dr;
COM = new mysqlcommand ("select * From db_table", myconn );
Dr = com. executereader ();
While (dr. Read ())
Textbox1.appendtext ("** =" + Dr ["*"] + "" + environment. newline );
Dr. Close ();
}