MySQL Database helper class

Source: Internet
Author: User

/** * Creator: CALVINR * Description: Database helper class **/namespacestudy.utilities.mysql_connect{/// <summary>    ///Summary description of Mysqlhelper/// </summary>     Public classMysqlhelper {PrivateMysqlconnection conn =NULL; PrivateMysqlcommand cmd =NULL; PrivateMysqldatareader SDR; PrivateMysqldataadapter SDA =NULL;  PublicMysqlhelper () {stringConnStr = configurationmanager.connectionstrings["connectstring"]. ConnectionString;//get the MySQL database connection stringconn =NewMysqlconnection (CONNSTR);//Database Connection        }        /// <summary>        ///Open Database Link/// </summary>        /// <returns></returns>        Privatemysqlconnection Open () {if(Conn. state = =connectionstate.closed) {conn.            Open (); }            returnConn; }        /// <summary>        ///Close Database Link/// </summary>        Private voidClose () {if(Conn. state = =ConnectionState.Open) {conn.            Close (); }        }        /// <summary>        ///perform additions and deletions without parameters to SQL statements or stored procedures/// </summary>        /// <param name= "Cmdtext" >Adding or deleting a string that changes to an SQL statement or stored procedure</param>        /// <param name= "ct" >Command Type</param>        /// <returns>the affected function</returns>         Public intExecuteNonQuery (stringcmdtext, CommandType CT) {            intRes; using(cmd =NewMysqlcommand (Cmdtext, Open ())) {Cmd.commandtype=CT; Res=cmd.            ExecuteNonQuery (); }            returnRes; }        /// <summary>        ///perform additions and deletions with parameters to SQL statements or stored procedures/// </summary>        /// <param name= "Cmdtext" >Adding or deleting a string that changes to an SQL statement or stored procedure</param>        /// <param Name= "Paras" >A collection of parameters assigned to a stored procedure or SQL</param>        /// <param name= "ct" >Command Type</param>        /// <returns>the affected function</returns>         Public intExecuteNonQuery (stringCmdtext, Mysqlparameter[] paras, CommandType CT) {            intRes; using(cmd =NewMysqlcommand (Cmdtext, Open ())) {Cmd.commandtype=CT; Cmd.                Parameters.addrange (paras); Res=cmd.            ExecuteNonQuery (); }            returnRes; }        /// <summary>        ///execute a query SQL statement or stored procedure without parameters/// </summary>        /// <param name= "Cmdtext" >a string that queries an SQL statement or stored procedure</param>        /// <param name= "ct" >Command Type</param>        /// <returns>query to DataTable object</returns>         PublicDataTable ExecuteQuery (stringcmdtext, CommandType CT) {DataTable dt=NewDataTable (); CMD=NewMysqlcommand (Cmdtext, Open ()); Cmd.commandtype=CT; using(SDR =cmd. ExecuteReader (commandbehavior.closeconnection)) {dt.            Load (SDR); }            returnDT; }        /// <summary>        ///execute a query SQL statement or stored procedure with parameters/// </summary>        /// <param name= "Cmdtext" >a string that queries an SQL statement or stored procedure</param>        /// <param Name= "Paras" >Parameter Collection</param>        /// <param name= "ct" >Command Type</param>        /// <returns></returns>         PublicDataTable ExecuteQuery (stringCmdtext, Mysqlparameter[] paras, CommandType CT) {DataTable dt=NewDataTable (); CMD=NewMysqlcommand (Cmdtext, Open ()); Cmd.commandtype=CT; Cmd.            Parameters.addrange (paras); using(SDR =cmd. ExecuteReader (commandbehavior.closeconnection)) {dt.            Load (SDR); }            returnDT; }        /// <summary>        ///executes the command that specifies the database connection string and returns a DataSet. /// </summary>        /// <param name= "strSQL" >a valid database connection string</param>        /// <returns>returns a dataset containing the result set</returns>         PublicDataSet ExecuteDataset (stringstrSQL) {DataSet DS=NewDataSet (); SDA=NewMysqldataadapter (strSQL, Open ()); Try{SDA.            Fill (DS); }            Catch(Exception ex) {Throwex; }            finally{Close (); }            returnds; }    }}

MySQL Database helper class

Related Article

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.