C # connect to the MySQL class

Source: Internet
Author: User
Csung connects to mysql install mysql-connector-net-5.0.6.zip and adds the reference connection string: Data Source = 127.0.0.1; user id = root; Pwd =; initial catalog = test; allow zero datetime = trueusing system; using system. collections. generic; using system. text; using system. data; using MySQL. data. mysqlclient; using system. io; using system. windows. forms; namespace windowsapplication1 {class dataconn {private mysqlconnection myconnection; private mysqlcommand Mycommand; private mysqldataadapter myadapter; private mysqltransaction mytransaction; // create a DB connection public void getconnection () {streamreader din = file. opentext ("textfile. ini "); string contstring = din. readline (); try {myconnection = new mysqlconnection (); myconnection. connectionstring = contstring; myconnection. open ();} catch (exception ex) {console. writeline (Ex); MessageBox. show ("connection failed! ") ;}}// Data Query Operation Public able executequery (string SQL) {datatable mytable; try {mycommand = myconnection. createcommand (); mycommand. commandtext = SQL; myadapter = new mysqldataadapter (mycommand); dataset myset = new dataset (); myadapter. fill (myset, "selectda"); mytable = myset. tables ["selectda"]; return mytable;} catch (exception ex) {console. writeline (Ex); mytable = new datatable (); MessageBox. s How ("data error! "); Return mytable ;}}// insert, delete, and update public Boolean executeupdate (string SQL) {try {mycommand = myconnection. createcommand (); mycommand. commandtext = SQL; mycommand. executenonquery (); If (mytransaction = NULL) {myconnection. close (); myconnection = NULL;} return true;} catch (exception ex) {If (mytransaction! = NULL) {mytransaction. rollback (); mytransaction = NULL; MessageBox. Show ("data error, transaction rollback is enabled! ");} Else if (myconnection = NULL) {MessageBox. Show (" enable transaction! ");} Else {MessageBox. Show (" an error occurred! ");} Console. writeline (Ex); return false ;}// create the transaction public void createtransaction () {try {mytransaction = myconnection. begintransaction ();} catch (exception ex) {console. writeline (Ex); MessageBox. show ("failed to enable transaction! ") ;}} // Submit the transaction public void committransaction () {try {If (mytransaction! = NULL) mytransaction. commit ();} catch (exception ex) {mytransaction. rollback (); console. writeline (Ex); MessageBox. show ("data error, transaction rollback is enabled! ") ;}Finally {myconnection. Close (); myconnection = NULL ;}}}}

 

Mysql-connector-Net:Http://mysql.isu.edu.tw/downloads/connector/net/5.2.html

 

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.