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