Beef Brisket Press Release system (b/s) in the SqlHelper

Source: Internet
Author: User

1. A brief description of the basics of SqlHelper:

SqlHelper is a. NET framework-based database operations component. The component contains a database operation method, SqlHelper has many versions, mainly in the first release of Microsoft SqlHelper class, later included in the Enterprise Library open source package. There is also a major versionof dbhelper. org open-source SqlHelper component, the advantage is simple, high performance, not only support SQL Server, but also support SQL Server, Oracle, Access, MySQL database, Also an open source project that offers free downloads.

SqlHelper is used to simplify the way you repeatedly write those database connections (SqlConnection), Sqlcommand,sqldatareader and so on. SqlHelper package is usually only needed to pass some parameters such as database connection string, SQL parameters, etc., you can access the database, very convenient.

The SqlHelper class is used to pass a set of static methods to encapsulate the data access functionality. The class cannot be inherited or instantiated, so it is declared as a non-inheriting class that contains a dedicated constructor. Each of the methods implemented in the SqlHelper class provides a consistent set of overloads . This provides a good pattern for executing commands using the SqlHelper class, while providing the necessary flexibility for the developer to choose how to access the data. The overloads of each method support different method parameters, so the developer can determine how the connection, transaction, and parameter information is passed.


2. Here is the main point about the experience of learning SqlHelper again:

In this time, b/s Beef Brisket news release system in the learning process, relative to the C/s study in the use of sqlhelper, slightly different, but a large part of the appearance of C/S. One of the main uses of SqlHelper is the assistant to the database, which executes the SQL statement or stored procedure. The application of database Assistant is mainly to facilitate the development and application of developers, reduce the amount of code to reach the effect of improving efficiency.


/* Created by: Wushlong * Created: 2015-07-15 17:12 * Description: Database helper class * All rights reserved: Personal */using system;using system.collections.generic;using system.li Nq;using system.text;using system.data;using system.data.sqlclient;using system.configuration;namespace DAL{Public        Class SQLHelper {private SqlConnection conn = null;        Private SqlCommand cmd = null;        Private SqlDataReader SDR = null; Public SQLHelper () {String connstr = configurationmanager.connectionstrings["ConnStr"].            ConnectionString;        conn = new SqlConnection (CONNSTR); Private SqlConnection Getconn () {if (conn. state = = connectionstate.closed) {Conn.            Open ();        } return conn; }///<summary>///Do not take parameters to delete or change SQL statements or stored procedures///</summary>//<param name= "Cmdte XT "> Additions to SQL statements or stored procedures </param>//<param name=" ct "> Command type </param>///<returns></ret Urns> public int ExecuteNonQuery (string cmdtext, CommandType ct) {int res;                try {cmd = new SqlCommand (Cmdtext, Getconn ());                Cmd.commandtype = ct; res = cmd.            ExecuteNonQuery ();            } catch (Exception ex) {throw ex; } finally {if (conn. state = = ConnectionState.Open) {Conn.                Close ();        }} return res; }///<summary>///execute with parameters or delete or change SQL statements or stored procedures///</summary>//<param name= "Cmdtex T "> Delete and change SQL statements or stored procedures </param>//<param name=" ct "> Command type </param>///<returns></retu rns> public int ExecuteNonQuery (string cmdtext, Sqlparameter[] paras, CommandType ct) {int re            S          using (cmd = new SqlCommand (Cmdtext, Getconn ()))  {cmd.commandtype = ct; Cmd.                Parameters.addrange (paras); res = cmd.            ExecuteNonQuery ();        } return res; }///<summary>//EXECUTE query SQL statements or stored procedures///</summary>//<param name= "Cmdtext" Query SQL statements or stored procedures </param>//<param name= "ct" > Command type </param>///<returns></returns&gt        ;            Public DataTable ExecuteQuery (string cmdtext, CommandType ct) {DataTable dt = new DataTable ();            cmd = new SqlCommand (Cmdtext, Getconn ());            Cmd.commandtype = ct; using (SDR = cmd. ExecuteReader (commandbehavior.closeconnection)) {dt.            Load (SDR);        } return DT; }///<summary>///EXECUTE query SQL statements or stored procedures with parameters///</summary>//<param name= "Cmdtext   "> Querying SQL statements or stored procedures </param>//<param Name=" paras "> Parameter collections </param>     <param name= "CT" > Command type </param>//<returns></returns> public DataTable Exec            Utequery (String cmdtext, Sqlparameter[] paras, CommandType ct) {datatable dt = new DataTable ();            cmd = new SqlCommand (Cmdtext, Getconn ());            Cmd.commandtype = ct; Cmd.            Parameters.addrange (paras); using (SDR = cmd. ExecuteReader (commandbehavior.closeconnection)) {dt.            Load (SDR);        } return DT; }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Beef Brisket Press Release system (b/s) in the SqlHelper

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.