. Net connection to Mysql encapsulation code can be called directly

Source: Internet
Author: User

Microsoft's visual studio does not have a built-in connection to Mysql driver, go to the Internet to download a mysql-connector-net-6.4.3 driver, and then the installation can be used.
The following is my encapsulated database connection class, which can be called directly.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. Data;
Using System. Data. SqlClient;
Using System. Configuration;
Using MySql. Data. MySqlClient;
Namespace DAL
{
Public class GetConnection
{
Private static MySqlConnection _ connection;
/// <Summary>
/// Obtain the Database Connection Bridge
/// </Summary>
Private static MySqlConnection Connection
{
Get
{
// String connectionString = ConfigurationManager. etettings ["ConnectionString"];
String connectionString = "server = localhost; user id = root; password = root; database = system; pooling = false ";
// Server = 222.222.222.222; port = 3306; uid = user; pwd =; database = basename; remotely connected
// String connectionString = "Data Source = 202.192.72.22; Initial Catalog = wwj; Persist Security Info = True; User ID = wwj; Password = wwj123 ";
If (_ connection = null)
{
_ Connection = new MySqlConnection (connectionString );
_ Connection. Open ();
}
If (_ connection. State = ConnectionState. Closed)
{
_ Connection. Open ();
}
If (_ connection. State = ConnectionState. Broken)
{
_ Connection. Close ();
_ Connection. Open ();
}
Return GetConnection. _ connection;
}
}
/// <Summary>
/// Obtain table data
/// </Summary>
/// <Param name = "SQL"> </param>
/// <Returns> </returns>
Public static MySqlDataReader GetDataRead (string SQL)
{
MySqlCommand command = new MySqlCommand (SQL, Connection );
MySqlDataReader read = command. ExecuteReader ();
Return read;
}
Public static int NoSelect (string SQL)
{
MySqlCommand command = new MySqlCommand (SQL, Connection );
Int row = command. ExecuteNonQuery ();
Return row;
}
Public static DataTable GetDataTable (string SQL)
{
MySqlCommand command = new MySqlCommand (SQL, Connection );
DataTable dt = new DataTable ();
MySqlDataAdapter sda = new MySqlDataAdapter (command );
Sda. Fill (dt );
Return dt;
}
/// <Summary>
/// Execute the SQL statement and return one row and one column ..
/// </Summary>
/// <Param name = "SQL"> SQL statement </param>
/// <Returns> </returns>
Public static string GetScalar (string SQL)
{
MySqlCommand command = new MySqlCommand (SQL, Connection );
Return command. ExecuteScalar (). ToString ();
}
}
}

For example, if you want to delete the object, you can call GetConnection. noSelect ("delete from UserInfo where Id = 1"); read a table in the database and call GetConnection. getDataTable ("select * from UserInfo"); easy to call.

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.