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