ASP. NET connection MySQL Database

Source: Internet
Author: User
Tags dsn

Method One:
Using MySQL's connector/net component, MySQL, which is a. NET private access component designed by MySQL to access the MySQL database for ADO. After you complete the component, you need to reference the component in your project, or you can add the following node directly within the <assemblies> node of the configuration file:

<add assembly= "Mysql.data, version=5.1.5.0, culture=neutral, publickeytoken=c5687fc88969c44d"/>
After you reference the namespace MySql.Data.MySqlClient in your program, you can begin the operation of connecting to the MySQL database, as shown in the following example:

1protected void Mysqlcon ()
2{
3//Database connection string is no different than connecting to SQL Server
4 string constr = "server=localhost; User Id=root;password=root;database=test ";
5
6//below use dedicated objects provided by MySQL Connector/net
7 Mysqlconnection mycon = new Mysqlconnection (CONSTR);
8 Mycon. Open ();
9 Mysqlcommand mycmd = new Mysqlcommand ("SELECT * from users", mycon);
Ten Mysqldatareader myreader = myCMD. ExecuteReader ();
One while (myreader. Read ())
12 {
if (myreader. HasRows)
14 {
Response.Write (myreader. GetString ("email") + "<br/>");
16}
17}
myreader. Close ();
Mycon. Close ();
20
21}
Method Two:
Use Odbc.net. In general, 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 the installation, you can use Odbc.net to connect to the MySQL database, the first need to introduce the SYSTEM.DATA.ODBC namespace in the program, the specific example is as follows:
1 public void Connect_odbc ()
2 {
3//MySQL ODBC DSN needs to be created beforehand.
4 string odbcstring = "dsn=mysql;";
5
6//string odbcstring = "Driver={mysql ODBC 3.51 DRIVER};" +
7//"SERVER=LOCALHOST;" +
8//"port=3306;" +//omit this setting when connecting to a local database
9//"database=test;" +
+//"uid=root;" +
One//"password=root;" +
//"option=3";
13
OdbcConnection odbcconn = new OdbcConnection (odbcstring);
Odbcconn.open ();
OdbcCommand odbccmd = New OdbcCommand ("SELECT * from users", Odbcconn);
OdbcDataReader myreader = Odbccmd.executereader ();
while (myreader. Read ())
19 {
if (myreader. HasRows)
21 {
Response.Write (myreader. GetString (0) + "<br/>");
23}
24}
myreader. Close ();
Odbcconn.close ();
27}
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.