"SQL Server" DBHelper is the C # database underlying package

Source: Internet
Author: User

Using system;using system.collections.generic;using system.linq;using system.text;using System.Configuration;using System.data.sqlclient;using system.data;namespace myhelper{//non-public, only DAL private reference;//encapsulation Execute SQL Server Add/delete to the auxiliary class, provide the appropriate execution SQL  Methods of statements and stored procedures//Bai NINGSU (http://www.cnblogs.com/baiboy/) static class SQLHelper {//Read connection string in configuration file static string CONNSTR = configurationmanager.connectionstrings["SQL"].        ConnectionString; <summary>////execute non-query SQL statement, return the number of affected rows, if perform non-delete Gerze return-1//</summary>//<param name= "sql ">sql statement </param>//<param Name=" paras "> Parameter array </param>//<returns> affects row count Res</ret urns> public static int ExecuteNonQuery (String sql, params sqlparameter[] paras) {int res =-            1; using (SqlConnection conn = new SqlConnection (connstr)) {using (SqlCommand cmd = new SqlCommand                (SQL, conn)) {Cmd.commaNdtype = CommandType.StoredProcedure; IF (paras! = NULL | | paras. Length > 0) {cmd.                    Parameters.addrange (paras); } conn.                    Open (); res = cmd.                ExecuteNonQuery ();        }} return res; }///<summary>///execute non-query SQL statement, return the number of affected rows, if perform non-delete Gerze return-1//</summary>//<param Name= "SQL" >sql statement </param>///<param Name= "paras" > Parameter array </param>//<returns> affects number of rows re             s</returns> public static int executenonparaquery (String sql) {int res =-1; using (SqlConnection conn = new SqlConnection (connstr)) {using (SqlCommand cmd = new Sqlco Mmand (SQL, conn)) {Conn.                     Open (); res = cmd.                 ExecuteNonQuery ();    }} return res;     }///<summary>///execute read data, return an object///</summary>//<param name= "SQL" >sql statement </param>//<param Name= "paras" > Parameter array </param>//<returns> Returns an object o</re   turns> public static Object ExecuteScalar (String sql, params sqlparameter[] paras) {object             o = null; using (SqlConnection conn = new SqlConnection (connstr)) {using (SqlCommand cmd = new Sqlcomma                     nd (SQL, conn)) {cmd.commandtype = CommandType.StoredProcedure; IF (paras! = null) {cmd.                     Parameters.addrange (paras); } conn.                     Open (); o = cmd.                 ExecuteScalar ();         }} return o; }///<summary>///EXECUTE Query SQL statement, return an object///</summary>//<param Name= "SQL" >sql statement </param>///<param Name= "Paras" > Query parameters </param>//<returns> return D         Atareader object </returns> public static SqlDataReader ExecuteReader (String sql, params sqlparameter[] paras)             {SqlConnection conn = new SqlConnection (CONNSTR); using (SqlCommand cmd = new SqlCommand (SQL, conn)) {cmd.                 CommandType = CommandType.StoredProcedure; IF (paras! = null) {cmd.                 Parameters.addrange (paras); } conn.                 Open (); try {return cmd.                 ExecuteReader (commandbehavior.closeconnection); } catch (Exception ex) {cmd.                     Dispose ();                 Throw ex;    }}}///<summary> Execute Query SQL statement, return a parameterless DataSet object///</summary>     <param name= "SQL" >sql statements </param>//<param Name= "Paras" ></param>//<             Returns> Returns a DataSet object </returns> public static DataSet Getdatasetnotpara (String sql) {             DataSet ds = new DataSet (); using (SqlConnection conn = new SqlConnection (connstr)) {using (SqlCommand cmd = new Sqlcomma nd (SQL, conn)) {//According to the parameters that are coming.                     Whether the decision is an SQL statement or a stored procedure cmd.commandtype = CommandType.StoredProcedure; Conn.                     Open (); Using (SqlDataAdapter SDA = new SqlDataAdapter (cmd)) {SDA.                     Fill (DS);         }}} return DS; }///<summary>///EXECUTE Query SQL statement, return a parameterless DataSet object///</summary>//<param N Ame= "SQL" >sql statement </param>///<param Name= "Paras" ></param>//<returns> return DataSet object </returns> public static DataTable Getdatatablenotpara (str             ing SQL) {datatable dt = new DataTable (); using (SqlConnection conn = new SqlConnection (connstr)) {using (SqlCommand cmd = new Sqlcomma nd (SQL, conn)) {//According to the parameters that are coming. Determines whether the SQL statement or the stored procedure conn.                     Open (); Using (SqlDataAdapter SDA = new SqlDataAdapter (cmd)) {SDA.                     Fill (DT);         }}} return dt; }///<summary>///EXECUTE Query SQL statement, return a DataSet object///</summary>//<param name=  "SQL" >sql statement </param>//<param Name= "Paras" > Query parameters </param>//<returns> return DataSet Object </returns> public static DataSet GetDataSet (String sql, params SqlparametEr[] paras) {DataSet ds = new DataSet (); using (SqlConnection conn = new SqlConnection (connstr)) {using (SqlCommand cmd = new Sqlcomma nd (SQL, conn)) {//According to the parameters that are coming.                     Whether the decision is an SQL statement or a stored procedure cmd.commandtype = CommandType.StoredProcedure; Add the parameter cmd.                     Parameters.addrange (paras); Conn.                     Open (); Using (SqlDataAdapter SDA = new SqlDataAdapter (cmd)) {SDA.                     Fill (DS);         }}} return DS; }///<summary>//Can execute SQL statements or stored procedures///</summary>//<param name= "text"         ;</param>//<param name= "CT" ></param>///<param name= "param" ></param> <returns></returns> public static DataTable ProcgettablE (String sql, params sqlparameter[] param) {datatable dt = new DataTable (); using (SqlConnection conn = new SqlConnection (connstr)) {using (SqlCommand cmd = new Sqlcomma nd (SQL, conn)) {//According to the parameters that are coming. Determines whether the SQL statement or the stored procedure is cmd.                     CommandType = CommandType.StoredProcedure; Add the parameter cmd.                     Parameters.addrange (param); Cmd. Parameters.Add ("@name", SqlDbType.NVarChar, 20).                     Value = param[0]; Cmd. Parameters.Add ("@pwd", SqlDbType.NVarChar, 20).                     Value = param[1]; Conn.                     Open (); Using (SqlDataAdapter SDA = new SqlDataAdapter (cmd)) {SDA.                     Fill (DT);         }}} return dt;     }//<summary>//For paging function///</summary>    <param name= "SQL" >sql statement </param>///<param Name= "paras" > Parameter array (displays index page and number of bars per page size) </pa ram>//<returns> Query Results </returns> public static DataTable getparatable (String sql, params Sq             Lparameter[] paras) {DataSet ds = new DataSet (); using (SqlConnection conn = new SqlConnection (connstr)) {using (SqlDataAdapter da = new SqlDa                         Taadapter (SQL, conn)) {if (paras! = null) { Da.                     SelectCommand.Parameters.AddRange (paras);                     } da.SelectCommand.CommandType = CommandType.StoredProcedure; Da.                 Fill (DS); }} return DS.         Tables[0]; }    }}

  

"SQL Server" DBHelper is the C # database underlying package

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.