The following articles mainly describe to you.. net to connect to the MySQL database. net to connect to the MySQL database. We mainly divide it into three parts. The following article describes the detailed content.
Http://dev.csdn.net/develop/article/73/73226.shtm
Method and example for connecting to the MySQL database
Method 1:
MySQL Connector/Net is an ADO. NET driver for MySQL
This component is a. NET access component designed for accessing the MySQL database by using ADO. NET.
After the component is installed, reference the namespace MySQL. Data. MySQLClient;
When using command line compilation: csc/r: MySQL. Data. dll test. cs
Method 2:
Access the MySQL database through ODBC
Download two components: the ODBC driver (MySQL Connector/ODBC (MyODBC) driver) of odbc.net and MySQL. The current version is 3.51.
After the installation is complete, you can access the MySQL database through ODBC
Method 3:
Use the MySQL access component released by CoreLab for. NET
After the installation is complete, reference the namespace: CoreLab. MySQL;
Run the command: csc/r: CoreLab. MySQL. dll test. cs
Access the MySQL database instance
Compilation command: csc/r: CoreLab. MySQL. dll/r: MySQL. Data. dll test. cs
- using System;
- using System.Net;
- using System.Text;
- using CoreLab.MySQL;
- using System.Data.Odbc;
- using MySQL.Data.MySQLClient;
- class ConnectMySQL
- {
- public void Connect_CoreLab()
- {
- string constr = "User Id=root;Host=localhost;Database=qing;password=qing";
- MySQLConnection mycn = new MySQLConnection(constr);
- mycn.Open();
- MySQLCommand mycm = new MySQLCommand("select * from shop",mycn);
- MySQLDataReader msdr = mycm.ExecuteReader();
- while(msdr.Read())
- {
- if (msdr.HasRows)
- {
- Console.WriteLine(msdr.GetString(0));
- }
- }
- msdr.Close();
- mycn.Close();
- }
- public void Connect_Odbc()
- {
- //string MyConString ="DSN=MySQL;UID=root;PWD=qing";
- string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" +
- "SERVER=localhost;" +
- "DATABASE=test;" +
- "UID=root;" +
- "PASSWORD=qing;" +
- "OPTION=3";
- OdbcConnection MyConn = new OdbcConnection(MyConString);
- MyConn.Open();
- OdbcCommand mycm = new OdbcCommand("select * from hello",MyConn);
- OdbcDataReader msdr = mycm.ExecuteReader();
- while(msdr.Read())
- {
- if (msdr.HasRows)
- {
- Console.WriteLine(msdr.GetString(0));
- }
- }
- msdr.Close();
- MyConn.Close();
- }
- public void Connect_Net()
- {
- string myConnectionString = "Database=test;Data Source=localhost;User Id=root;Password=qing";
- MySQLConnection mycn = new MySQLConnection(myConnectionString);
- mycn.Open();
- MySQLCommand mycm = new MySQLCommand("select * from hello",mycn);
- MySQLDataReader msdr = mycm.ExecuteReader();
- while(msdr.Read())
- {
- if (msdr.HasRows)
- {
- Console.WriteLine(msdr.GetString(0));
- }
- }
- msdr.Close();
- mycn.Close();
- }
- public static void Main()
- {
- ConnectMySQL ms = new ConnectMySQL();
- ms.Connect_CoreLab();
- ms.Connect_Odbc();
- Connect_Net();
- }
- }
The above content is an introduction to. net methods and examples for connecting to the MySQL database.