Manipulating SQL database Help classes

Source: Internet
Author: User
Tags throw exception

Using system;using system.collections.generic;using system.linq;using system.text;using System.Data.SqlClient;using System.data;namespace datebase{public class Helper {//<summary>///Definition SqlConnection/        </summary> public static SqlConnection conn = null;        <summary>//Database connection public static string str = configurationmanager.appsettings["Mysqlconncetion"]; </summary> public static string str = "Data source=.;i Nitial Catalog=myoffice;        User id=www_jy;password=***************** ";        <summary>//things rolled back///</summary> public static sqltransaction trans = null;            <summary>///Open database connection///</summary> public static SqlConnection Connection {                        Get {try {if (conn = = null) { conn = new SqlConnection (str); Conn.                    Open (); } else if (conn. state = = connectionstate.closed) {Conn.                    Open (); } else if (conn. state = = Connectionstate.broken) {Conn.                        Close (); Conn.                    Open ();                }} catch (Exception e) {//throws an error}            Return conn;        }}///<summary>//Open things///</summary> public static void Opentrans ()            {try {trans = connection.begintransaction ();        } catch (Exception e) {//Throw exception}}//<summary>       Things submitted///</summary> public static void CommitTrans () {trans.commit (); }///<summary>//things rolled back///</summary> public static void RollbackTrans () {trans.        Rollback (); }///<summary>//<param name= "SQL" ></param&        Gt            <returns></returns> public static int Getexecute (String sql) {int num = 0;                try {SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.                Transaction = trans; num = cmd.            ExecuteNonQuery ();            } catch (Exception e) {//throws exception RollbackTrans (); Finally {//closes the database connection conn.            Close ();        } return num; }///<summary>///parameter execution increment, delete, change operation///</summary>//<param name= "SQL" ></p aram>//<param name= "pIs "></param>///<returns></returns> public static int Getexecute (String sql, Sqlparam            Eter[] pare) {int num = 0;                try {SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.                Parameters.addrange (PARE); Cmd.                Transaction = trans; num = cmd.            ExecuteNonQuery ();            } catch (Exception e) {//throws exception RollbackTrans (); Finally {//closes the database connection conn.            Close ();        } return num;         }///<summary>///Check number////</summary>//<param name= "SQL" ></param> <returns></returns> public static int getexecutescalar (String sql) {int            num = 0;           try {SqlCommand cmd = new SqlCommand (sql, Connection);     num = Convert.ToInt32 (cmd.            ExecuteScalar ());                } catch (Exception e) {//throws an exception} finally { Close the database connection conn.            Close ();        } return num;         }///<summary>/////</summary>//<param name= "SQL" ></param> <param name= "pare" ></param>///<returns></returns> public static int Ge            Texecutescalar (String sql, sqlparameter[] pare) {int num = 0;                try {SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.                Parameters.addrange (PARE); num = Convert.ToInt32 (cmd.            ExecuteScalar ());                } catch (Exception e) {//throws an exception} finally { Close the database connection conn.            Close (); } return Num; }///<summary>//Lookup database//</summary>//<param name= "SQL" ></param&gt        ; <returns></returns> public static DataSet GetDataSet (String sql) {DataSet ds = N            EW DataSet ();                try {SqlDataAdapter adapter = new SqlDataAdapter (SQL, Connection); Adapter.            Fill (DS);                } catch (Exception e) {//throws an exception} finally { Close the database connection conn.            Close ();        } return DS; }///<summary>///Search Database///</summary>//<param name= "SQL" ></param&        Gt <param name= "pare" ></param>///<returns></returns> public static DataSet Getdat            ASet (String sql, sqlparameter[] pare) {DataSet ds = new DataSet ();      Try      {SqlCommand cmd = new SqlCommand (sql, Connection); Cmd.                Parameters.addrange (PARE);                SqlDataAdapter adapter = new SqlDataAdapter (cmd); Adapter.            Fill (DS);                } catch (Exception e) {//throws an exception} finally { Close the database connection conn.            Close ();        } return DS; }    }}

Manipulating SQL database Help classes

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.