C # connect to the MYSQL database, including adding, deleting, querying, and modifying _ MySQL

Source: Internet
Author: User
C # connect to the MYSQL database, including adding, deleting, querying, and modifying C # connect to the MYSQL database, including adding, deleting, querying, modifying, and modifying!

Public class DBLINK

{

Public MySqlConnection GetConn ()

{

MySqlConnection mysqlconn = new MySqlConnection ("server = localhost; pwd = 123; user = root; database = pfms; charset = 'gb2312 '"); // 1, server 2 Mysql password 3 mysql username 4 database name 5 database encoding method

Return mysqlconn;

}

}

Public class SQLEXQ: DBLINK

{

/// Query operation

Public DataTable Select (string SQL)

{

MySqlConnection mysqlconn = null;

MySqlDataAdapter sda = null;

DataTable dt = null;

Try

{

Mysqlconn = base. GetConn ();


Sda = new MySqlDataAdapter (SQL, mysqlconn );

Dt = new DataTable ();

Sda. Fill (dt );


Return dt;

}

Catch (Exception)

{


Throw;

}


}


/// Add operation

Public int Add (string SQL)

{


MySqlConnection conn = null;

MySqlCommand cmd = null;


Try

{

Conn = base. GetConn ();

Conn. Open ();

Cmd = new MySqlCommand (SQL, conn );

Int I = cmd. ExecuteNonQuery ();

Conn. Close ();

Return I;

}

Catch (Exception)

{


Throw;

}


}

// Modify

Public int Change (string SQL)

{


MySqlConnection conn = null;

MySqlCommand cmd = null;


Try

{

Conn = base. GetConn ();

Conn. Open ();

Cmd = new MySqlCommand (SQL, conn );

Int I = cmd. ExecuteNonQuery ();

Conn. Close ();

Return I;


}

Catch (Exception)

{


Throw;

}


}

// Delete

Public int Delete (string SQL)

{


MySqlConnection conn = null;

MySqlCommand cmd = null;


Try

{

Conn = base. GetConn ();

Conn. Open ();

Cmd = new MySqlCommand (SQL, conn );

Int I = cmd. ExecuteNonQuery ();

Conn. Close ();

Return I;


}

Catch (Exception)

{


Throw;

}


}


}


}

Usage:

// Query

String sql1 = "select max (inumber) + 1 as num from income"; // query SQL statements

DataTable dt = mysql. Select (sql1); // put it in the data table

If (dt. Rows. Count> 0) // whether data exists

{

For (int I = 0; I <dt. Rows. Count; I ++)

{

Ia. textBox_bh.Text = dt. Rows [I] ["num"]. ToString (); // display in the edit box

}

}

For other similar statements, you only need to change the SQL statement! For is a specific operation to read, store, delete, or modify the data!


By the way, I use MYSQL. data to connect to the database, so I need to use using MySql. Data. MySqlClient; // import the MySql package! You also need to import MySql. Data. dll to the application of the C # Project! Controls; http://download.csdn.net/my

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.