C # operate mysql. Data. dll

Source: Internet
Author: User
Using system; using system. collections. generic; using system. LINQ; using system. text; using system. data; using MySQL. data. mysqlclient; using system. io; namespace comback {class serverdatabase {mysqlconnection conn; string connstring = "Persist Security info = false; database = tldb; server = 127.0.0.1; user id = root; Pwd = 123456 "; private void openconn () {try {conn = new mysqlconnection (); Conn. connectionstring = Connstring; Conn. open ();} catch (exception ee) {system. windows. forms. messageBox. show ("An error occurred while opening the database:" + ee. message );}} /// <summary> /// modify the function /// </Summary> /// <Param name = "SQL"> modify statement </param> /// <returns> </returns> Public int excuteuser (string SQL) {mysqlcommand cmd = NULL; mysqltransaction trans = NULL; int COUNT = 0; try {If (conn = NULL | Conn. state = connectionstate. closed) {openconn ();} Cmd = Conn. createcommand (); cmd. commandtext = SQL; // create transaction trans = Conn. begintransaction (); Count = cmd. executenonquery (); // transaction commit trans. commit ();} catch {// transaction rollback trans. rollback ();} finally {If (Conn! = NULL & (conn. State! = Connectionstate. closed) {cmd. dispose (); Conn. close (); Conn. dispose () ;}} return count ;}// check the logon user's Public String checkuser (string username, string password) {string role = ""; mysqlcommand cmd = NULL; mysqldatareader reader = NULL; try {If (conn = NULL | Conn. state = connectionstate. closed) {openconn ();} cmd = Conn. createcommand (); cmd. commandtext = "select role from tluser where Username = '"+ Username +"' and Password = '"+ password +"' "; reader = cmd. executereader (); If (reader. hasrows) {If (reader. read () {role = reader. getstring ("role") ;}} else {role = "" ;}} catch (exception ee) {system. windows. forms. messageBox. show ("exception when the server checks the validity of the logged-on user:" + ee. message);} finally {If (Conn! = NULL & (conn. State! = Connectionstate. closed) {If (reader. isclosed) {reader. close (); reader. dispose ();} cmd. dispose (); Conn. close (); Conn. dispose () ;}} return role ;}}}
 
 
/// <Summary> /// obtain the dataset /// </Summary> /// <Param name = "ds"> </param> /// <Param name = "SQL"> </param> // <returns> </returns> Public dataset getds (Dataset ds, string SQL) {// open the connection getconnection (); try {// query result mysqldataadapter MDA = new mysqldataadapter (SQL, Conn); MDA. fill (DS, "data");} catch (system. exception e) {MessageBox. show ("getds () exception:" + E. message);} finally {Conn. close (); Conn. dispose ();} return Ds ;}
 

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.