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 ;}