. NET connection MySQL encapsulation Class code can be directly called _ Practical tips

Source: Internet
Author: User
Microsoft's Visual Studio does not have its own driver to connect to MySQL, to download a mysql-connector-net-6.4.3 driver on the Internet, and then install it to use.
The following is a class that I encapsulate to connect the database, call directly.
Copy Code code 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>
Getting the database connection bridge
</summary>
private static Mysqlconnection Connection
{
Get
{
String connectionString = configurationmanager.appsettings["connectionString"];
String connectionString = "Server=localhost;user id=root; Password=root; Database=system; Pooling=false ";
server=222.222.222.222;port=3306;uid=user;pwd=;d atabase=basename; remote 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>
Get 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>
Executes the SQL statement, returning a row of columns ...
</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 perform a deletion, you can call Getconnection.noselect ("delete from UserInfo where id=1"), read one of the tables in the database, and you can call getconnection.getdatatable ("SELECT * from UserInfo"); it's convenient to call.

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.