C # connect MySQL database from start to abort

Source: Internet
Author: User
Tags connectionstrings

Directly open.

1, only need to Mysql.data.dll download good after free place.

2, add a reference to the new project.

Browse to select the downloaded Mysql.data.dll address. After determination.

3, after the successful addition, the namespace will be able to enter the using MySql.Data.MySqlClient.

At this point, you can successfully connect to MySQL.

Steps:

<connectionStrings>
<add name= "Mysqlconnstr" connectionstring= "server=localhost;database=tset; User=root; password=****; Use Procedure bodies=false; Charset=utf8; Allow Zero datetime=true; Pooling=false; Max Pool size=50; "/>
</connectionStrings>

Configuration XML. As with SQL.

2. Get a database connection

<summary>
Get database connection
</summary>
<returns></returns>

Public Mysqlconnection Getconn () {
String connstr = system.configuration.configurationmanager.connectionstrings["Mysqlconnstr"]. ConnectionString;
Mysqlconnection = new Mysqlconnection (CONNSTR);
return mysqlconnection;
}

3. Perform data operations

<summary>
Execute Query
</summary>
<param name= "Commtext" ></param>
<returns></returns>

Public Mysqldatareader ExecuteNonQuery (String commtext) {

Mysqlconnection Mysqlcon = This.getconn ();
Mysqlcon. Open ();
Mysqlcommand Mysqlcommand = new Mysqlcommand ();
Mysqlcommand.connection = Mysqlcon;
Mysqlcommand.commandtext = Commtext;
Mysqldatareader mysqldatareader = null;
Try
{
Mysqldatareader = Mysqlcommand.executereader ();
}
catch (Exception ex)
{

}
Finally
{
Mysqlcon. Close ();
}
return mysqldatareader;
}

public void Executenullquery (String commtext) {
Mysqlconnection Mysqlcon = This.getconn ();
Mysqlcon. Open ();
Mysqlcommand Mysqlcommand = new Mysqlcommand ();
Mysqlcommand.connection = Mysqlcon;
Mysqlcommand.commandtext = Commtext;
Perform actions
Mysqlcommand.executenonquery ();
Mysqlcon. Close ();
}

over~

C # connect MySQL database from start to abort

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.