. Net connection to MySQL database reprinted

Source: Internet
Author: User
Tags sql server connection string

Method 1:
Use the MySQL connector/NET Component released by MySQL, which is a. Net dedicated access component designed by MySQL for ADO. Net to access the MySQL database. After the component is completed, you must reference the component in the project. You can also add the following nodes to the <assemblies> node in the configuration file:

<Add Assembly = "mysql. Data, version = 5.1.5.0, culture = neutral, publickeytoken = c5687fc88969c44d"/>
Then, reference the namespace mysql. Data. mysqlclient in the program to connect to the MySQL database. For example:
 

1 protected void mysqlcon ()
2 {
3 // The database connection string is no different from the SQL server connection string
4 string constr = "Server = localhost; user id = root; Password = root; database = test ";
5
6 // use the dedicated object provided by MySQL connector/net below
7 mysqlconnection mycon = new mysqlconnection (constr );
8 mycon. open ();
9 mysqlcommand mycmd = new mysqlcommand ("select * from users", mycon );
10 mysqldatareader myreader = mycmd. executereader ();
11 while (myreader. Read ())
12 {
13 if (myreader. hasrows)
14 {
15 response. Write (myreader. getstring ("email") + "<br/> ");
16}
17}
18 myreader. Close ();
19 mycon. Close ();
20
21}
Method 2:
Use ODBC. net. Generally, the ODBC. Net dataprovider is part of the standard. NET Framework (Version 1.1 and later), so it will be automatically installed with the latter. Once ODBC. NET is installed, You need to download the ODBC driver for MySQL: MySQL connector/ODBC. The latest version is 3.51. After installation, You Can Use ODBC. Net to connect to the MySQL database. First, you need to introduce the system. Data. ODBC namespace in the program. The specific example is as follows:

1 Public void connect_odbc ()
2 {
3 // create a MySQL odbc dsn in advance.
4 string odbcstring = "DSN = MySQL ;";
5
6 // string odbcstring = "driver = {mysqlodbc 3.51 driver};" +
7 // "Server = localhost;" +
8 // "Port = 3306;" + // This setting can be omitted when you connect to the local database
9 // "database = test;" +
10 // "uid = root;" +
11 // "Password = root;" +
12 // "option = 3 ";
13
14 odbcconnection odbcconn = new odbcconnection (odbcstring );
15 odbcconn. open ();
16 odbccommand odbccmd = new odbccommand ("select * from users", odbcconn );
17 odbcdatareader myreader = odbccmd. executereader ();
18 while (myreader. Read ())
19 {
20 if (myreader. hasrows)
21 {
22 response. Write (myreader. getstring (0) + "<br/> ");
23}
24}
25 myreader. Close ();
26 odbcconn. Close ();
27}

This article from: PQ xiuxiu Network (http://www.pqshow.com/) detailed source reference: http://www.pqshow.com/program/aspnet/200909/10107.html

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.