C # example of MYSQ connection (Comprehensive)

Source: Internet
Author: User

C # example of MYSQ connection (Comprehensive)

Find MySQLDriver. dll in the installation folder, and add the MySQLDriver. dll to the project.

(ADD) insert data:
Using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (localhost, housing, root, root ). asString) {conn. open (); // SQL statement string SQL = insert into tbl_sysuser (usercode, username, password, usertype, isActived) values (c02, Cai qianqian, w1251314, real estate Bureau administrator, YES ); // prevent garbled characters MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); // execute the SQL statement MySQLCommand cmd = new MySQLCommand (SQL, conn); // return the number of affected rows int number = cmd. executeNonQuery (); // closes the database conn. close (); Console. writeLine (affected number of rows: + number );}

(Delete) delete data

 

 

Using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (localhost, housing, root, root ). asString) {conn. open (); // SQL statement string SQL = delete from tbl_sysuser where usercode = c02; // prevent garbled MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); // execute the SQL statement MySQLCommand cmd = new MySQLCommand (SQL, conn); // return the number of affected rows int number = cmd. executeNonQuery (); Console. writeLine (affected number of rows: + number); // closes the database conn. close ();}

(Change) modify data [injection value]

 

 

 

Using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (localhost, housing, root, root ). asString) {conn. open (); // prevent garbled characters MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); // SQL statement string SQL = update tbl_sysuser set isActived = @ isActived where id = @ id; // execute the SQL statement MySQLCommand cmd = new MySQLCommand (SQL, conn); // The injection value cmd. parameters. add (new MySQLParameter (@ isActived, YES); cmd. parameters. add (new MySQLParameter (@ id, 1); // returns the number of affected rows int number = cmd. executeNonQuery (); Console. writeLine (affected number of rows: + number); // closes the database conn. close ();}

(Change) modify data

 

 

Using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (localhost, housing, root, root ). asString) {conn. open (); // SQL statement string SQL = update tbl_sysuser set isActived = YES where id = 1; // prevent garbled MySQLCommand commn = new MySQLCommand (set names gb2312, conn ); commn. executeNonQuery (); // execute the SQL statement MySQLCommand cmd = new MySQLCommand (SQL, conn); // return the number of affected rows int number = cmd. executeNonQuery (); Console. writeLine (affected number of rows: + number); // closes the database conn. close ();}

(Query) query data:

 

 

Using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (localhost, housing, root, root ). asString) {conn. open (); // prevent garbled characters MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); // SQL statement string SQL = select * from tbl_sysuser; // query MySQLDataAdapter mda = new MySQLDataAdapter (SQL, conn) using the DataAdapter ); // The queried data exists in the able. DataTable can be understood as a virtual table. One row in the DataTable table is a record, and one column is a database Field DataTable dt = new DataTable (); mda. fill (dt); for (int I = 0; I <dt. rows. count; I ++) {Console. writeLine (extracted row: + dt. rows [I] [usercode] + | + dt. rows [I] [username] + | + dt. rows [I] [password] + | + dt. rows [I] [usertype] + | + dt. rows [I] [isActived]);} // closes the database conn. close ();}

(Query) query data 2

 

 

Using (MySQLConnection conn = new MySQLConnection (new MySQLConnectionString (localhost, housing, root, root ). asString) {conn. open (); // prevent garbled characters MySQLCommand commn = new MySQLCommand (set names gb2312, conn); commn. executeNonQuery (); // SQL statement string SQL = select * from tbl_sysuser; MySQLCommand cmd = new MySQLCommand (SQL, conn); MySQLDataReader reader = cmd. executeReaderEx (); while (reader. read () {if (reader. hasRows) {Console. writeLine (usercode: + reader. getString (1) + -- username: + reader. getString (2) ;}/// closes the database conn. close ();}


 

 

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.