Analysis of C # web access to mysql database-Summary

Source: Internet
Author: User

This article summarizes some knowledge points of web access to the mysql database in C #. For more information, see

Basic comparison

Usage Usage Advantages and disadvantages Installation required? Required dll URL
Reference Method Reference in program At the beginning of the program, determine to use MySql and add references in the early stage. In most cases, it is used in class files. Most of them are used in class files with post code such as aspx and ashx.

It can be installed, or dll can be directly referenced.

Connector/Net 6.7.4

Web. config reference The maintenance interface later finds that the program needs to be used, so it is not convenient to change the original program reference. Most of the time, it is used on files such as aspx and ashx without post code.
ODBC Mode Configure data sources in the system You can use

Applicable to page files (aspx, ashx), and class files with post code (aspx. cs, ashx. cs). When calling such programs, most odbc objects of ado.net are used.

Required

Connector/ODBC


I. Reference
1.1 reference in program
Prerequisites: (use one method)
1. In the web program, add reference-assembly-extension in reference-, the premise is that the mysql-connector-net
2. manually download MySql. Data. dll and add reference

Note: This method is generally used for page files aspx (aspx. cs) and ashx (ashx. cs) with "Post code)
Usage:
1. Add reference: using MySql. Data. MySqlClient;
2. Now the page can be used.
Test code:

Copy codeThe Code is as follows:
System. Data. DataSet ds = new System. Data. DataSet ();
String connStr = @ "Database = testss; Data Source = 172.160.234; User Id = root; Password = shinevod ";
String sqlList = "select * from sms_outbox ";
Try
{
Using (MySql. Data. MySqlClient. MySqlDataAdapter dataAapter = new MySql. Data. MySqlClient. MySqlDataAdapter (sqlList, connStr ))
{
DataAapter. Fill (ds );
Response. Write (ds. GetXml ());
}
}
Catch
{}


1.2 web. config reference
Prerequisites: (use one method)
1. In the web program, add reference-assembly-extension in reference-, the premise is that the mysql-connector-net
2. manually download MySql. Data. dll and add references (this is usually convenient and does not require installation)
Note: If a project has a sudden demand for a database mysql interface, you cannot install the program on the server (for fear of affecting other software ), it is generally used for webpage files without code backend, such as aspx and ashx.
Usage:
1. manually download MySql. Data. dll and place it under the bin of the website (if mysql-connector-net is not installed)
2. Add nodes to the web. config file of the website (the version number is changed according to your own usage)

Copy codeThe Code is as follows:
<System. web>
<Compilation debug = "true" targetFramework = "4.0">
<Assemblies>
<Add assembly = "MySql. Data, Version = 6.6.4.0, Culture = neutral, PublicKeyToken = C5687FC88969C44D"/>
</Assemblies>
</Compilation>
</System. web>


3. Now the webpage can be used.

Copy codeThe Code is as follows:
System. Data. DataSet ds = new System. Data. DataSet ();
String connStr = @ "Database = testss; Data Source = 172.160.234; User Id = root; Password = shinevod ";
String sqlList = "select * from sms_outbox ";
Try
{
Using (MySql. Data. MySqlClient. MySqlDataAdapter dataAapter = new MySql. Data. MySqlClient. MySqlDataAdapter (sqlList, connStr ))
{
DataAapter. Fill (ds );
Response. Write (ds. GetXml ());
}
}
Catch
{}


Ii. ODBC
2.1 configure the system data source
Prerequisites: Download: http://dev.mysql.com/downloads/connector/odbc /.
Note: the version number used during download and the number of operating systems (mainly windows)
Usage:
1. Add a System Data Source
Win7 (similar to other systems): "Control Panel"-"Management Tools"-"Data Source (ODBC)"-"User DSN-" add



2. program call

Copy codeThe Code is as follows:
System. Data. DataSet ds = new System. Data. DataSet ();
String str = @ "DSN = mysqltest"; // sets the Connection attribute and uses the MySql DSN
String sqlList = "select * from sms_outbox ";
Try
{
Using (System. Data. Odbc. OdbcDataAdapter dataAdapter = new System. Data. Odbc. OdbcDataAdapter (sqlList, str ))
{
DataAdapter. Fill (ds );
Response. Write (ds. GetXml ());
}
}
Catch (Exception ex)
{Response. Write (ex. ToString ());}


2.2 calls the driver in the program
Prerequisites: Download: http://dev.mysql.com/downloads/connector/odbc /.
Note: the version number used during download and the number of operating systems (mainly windows)
Usage:
1. After odbc is successfully installed, the program calls

Copy codeThe Code is as follows:


System. Data. DataSet ds = new System. Data. DataSet ();
String strCon = @ "Driver = MySql ODBC 5.2 Unicode Driver; Server = 172.160.234; Database = testss; UID = root; pwd = shinevod ";
String sqlList1 = "select * from sms_outbox ";
Try
{
Using (System. Data. Odbc. OdbcDataAdapter dataAdapter = new System. Data. Odbc. OdbcDataAdapter (sqlList1, strCon ))
{
DataAdapter. Fill (ds );
Response. Write (ds. GetXml ());
}
}
Catch (Exception ex)
{Response. Write (ex. ToString ());}

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.