This article mainly introduces 2 methods and examples of asp.net connection MySQL database, use MySQL official component and odbc.net, need friend can refer to under
Method One: Use MySQL official component using MySQL to launch the MySQL connector/net component, this component is MySQL for ado.net access MySQL database design of. Net Private Access Components. After you complete the component, you need to reference this component in your project, or you can add the following node directly within the < assemblies> node of the configuration file: Code as follows: <add assembly= "Mysql.data , version=5.1.5.0, culture=neutral, publickeytoken=c5687fc88969c44d "/> After referencing the namespace MySql.Data.MySqlClient in the program, you can begin the operation of connecting to the MySQL database, as follows: The code is as follows: protected void Mysqlcon () { The //database connection string does not differ from the connection to SQL Server string constr = "server=localhost; User Id=root;password=root;database=test "; //Use the dedicated object provided by MySQL connector/net mysqlconnection mycon = new Mysqlconnection (CONSTR); mycon. Open (); mysqlcommand mycmd = new Mysqlcommand ("SELECT * from users", mycon); Mysqldatareader myreader = myCMD. ExecuteReader (); while (myreader. Read ()) { if (myreader. HasRows) { Response.Write (myreader). GetString ("email") + "<br/>"); } } myreader. Close (); Mycon. Close (); } Method two: Using odbc.net generally speaking, odbc.net Dataprovider is part of the standard. NET Framework (version 1.1 and above), so it will be installed automatically with the latter. Once you have confirmed that the Odbc.net installation is complete, you will need to download the ODBC driver for MySQL: MySQL CONNECTOR/ODBC, currently the latest version is 3.51. After installation, you can use Odbc.net to connect to the MySQL database, first need to introduce the SYSTEM.DATA.ODBC namespace in the program, the specific example is as follows: The code is as follows: public void Connect_odbc () { //need to create MySQL ODBC DSN beforehand. string odbcstring = "dsn=mysql;"; & Nbsp;//string odbcstring = "Driver={mysql ODBC 3.51 DRIVER};" + // "server=localhost;" + // "port=3306" + //This setting can be omitted when connecting to the local database // "Databas e=test; "+ / " uid=root; "+ // " Password=root "+ // "option=3"; OdbcConnection odbcconn = new OdbcConnection (odbcstring); Odbcconn.open (); ODBCCOmmand odbccmd = New OdbcCommand ("SELECT * from users", Odbcconn); OdbcDataReader myreader = Odbccmd.executereader (); while (myreader. Read ()) { if (myreader. HasRows) { Response.Write (myreader. GetString (0) + "<br/>"); } } myreader. Close (); Odbcconn.close (); }