Mysql database connection method (. net), mysql database connection
1. connect through ado.net (the database connection string is Chinese and does not seem to be usable)
MySql. Data. dll needs to be added (available by installing the mysql-connector-net-6.8.3.mis)
Reference MySql. Data. dll
Call Method
String connectionString = "server = localhost; port = 3306; database = dbtest; uid = root; pwd = 111 ;"
MySqlConnection conn = new MySqlConnection (connectionString ))
Conn. Open ();
2. Use ODBC
32-bit installation mysql-connector-odbc-5.1.5-win32.msi for mysql
64-bit mysql installation mysql-connector-odbc-5.1.12-winx64.msi
For a 32-bit operating system, you can directly add a DSN to the ODBC manager. For a 64-bit operating system, if the mysql version is 64-bit, you can directly add a DSN, mysql is 32-bit, to add a DSN, Open C: \ Windows \ SysWOW64 \ odbcad32.exe. After adding the package, you can add a test in VS> View> server resource manager to check whether the test is successful.
32-bit ODBC Configuration Manager
String constr = "Driver = {MySQL ODBC 5.1 Driver}; Server = localhost; Port = 3306; Database = Chinese; User = root; Password = 111; Option = 3 ;";
OdbcConnection conn = new OdbcConnection (constr );
Conn. Open ();
3. Use MySQLDriverCS. dll (it cannot be used in 64-bit systems, and there is no 64-bit dll yet)
Directly reference MySQLDriverCS. dll (available on the Internet)
Using MySQLDriverCS;
MySQLConnection conn = new MySQLConnection (new MySQLConnectionString ("localhost", "test", "root", ""). AsString );
Conn. Open ();
Can I connect to the mysql database?
Actually. net is not so connected to the mysql database, it is very clumsy, it is described in books, the actual project rarely do this, download a MySqlClient from the Internet. dll, introduce the namespace of this assembly when writing the program, using MySqlClient; then you can reference the functions in the Assembly. MySqlCommand, MySqlDataAdapter, and MySqlConnection can be used at will, very convenient.
Recently, I just got started with net and used aspnet mvc3 to implement a registration and login module. How does mysql connect to the database?
Query Method
Public static DataSet ExecuteMySql (string strSQL)
{
// Make some changes here
MySqlConnection con = new MySqlConnection ("Host = 192.168.0.10; Port = 3306; User Id = root; Password = 1234; Persist Security Info = True; Database = smsprodb_mysql ");
MySqlDataAdapter da = new MySqlDataAdapter (your SQL statement, con );
DataSet ds = new DataSet ();
Da. Fill (ds );
Con. Close ();
Return ds;
}
Add, delete, and modify Methods
Public static DataSet UpdataMySql (string strSQL)
{
// Make some changes here
MySqlConnection con = new MySqlConnection ("Host = 192.168.0.10; Port = 3306; User Id = root; Password = 1234; Persist Security Info = True; Database = smsprodb_mysql ");
MySqlcommand sqlcom = new MySqlcommand ();
Sqlcom. Connection = con;
Sqlcom. CommandString = "Your SQL statement ";
Sqlconnect. open ();
DataReader dr = sqlcom. ExecuteReader ();
Int x = sqlcom. ExecuteNonQuery ();
Con. Close ();
}