Asp. NET connection to MySQL database 2 methods and examples

Source: Internet
Author: User
Tags dsn odbc mysql database

  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 ();  }  
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.