SQLHelper in niuyun news publishing system (B/S) and niuyun News Publishing System
1. A brief introduction to the basic knowledge of SQLHelper:
SqlHelper is A. NET Framework-based database operation component. The component contains the database operation method. SqlHelper has many versions, mainly the SqlHelper class released at the beginning of Microsoft, which is later included in the Enterprise Library open-source package. Another major version is dbhelper.org's open-source sqlhelper component, which features simplicity and high performance. It not only supports sqlserver, but also supports sqlserver, oracle, access, and Mysql databases. It is also an open-source project, free Download is provided.
SqlHelper is used to simplify the repeated Writing of database connections (SqlConnection), SqlCommand, SqlDataReader, and so on. After SqlHelper is encapsulated, you only need to input some parameters to the method, such as the database connection string and SQL parameters, to access the database, which is very convenient.
The SqlHelper class is used to encapsulate data access through a set of static methods. This class cannot be inherited or instantiated, so it is declared as a non-inherited class that contains a dedicated constructor. Each method implemented in the SqlHelper class provides a set of consistent overloading. This provides a good way to use the SqlHelper class to execute commands. It also provides necessary flexibility for developers to select a way to access data. Each method overload supports different method parameters, so developers can determine how to pass connection, transaction, and parameter information.
2. Here we will mainly talk about how to learn SQLHelper again:
Compared with SQLHelper, which is used in C/S learning, but most of them are similar to C/S. SQLHelper is mainly used as a database connection assistant to execute SQL statements or stored procedures. The application of the Database Assistant mainly aims to facilitate the development and application of developers and reduce the amount of code to improve efficiency.
/** Created by: Wu shilong * Creation Time: * Description: Database Assistant class * All Rights Reserved: individual */using System; using System. collections. generic; using System. linq; 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> /// execute the SQL statement or stored procedure without parameters. /// </summary> /// <param name = "plain text"> Add/delete modify an SQL statement or stored procedure </param> /// <param name = "ct"> command type </param> /// <returns> </returns> public int ExecuteNonQuery (string plain text, commandType ct) {int res; try {cmd = new SqlCommand (plain text, GetConn (); cmd. commandType = ct; res = cmd. executeNonQuery ();} catch (Exception ex) {throw ex;} finally {if (conn. state = ConnectionState. open) {conn. close () ;}} return res ;} /// <summary> /// execute the SQL statement or stored procedure with parameters. /// </summary> /// <param name = "plain text"> add, delete, and modify SQL statement or stored procedure </param> /// <param name = "ct"> command type </param> /// <returns> </returns> public int ExecuteNonQuery (string plain text, sqlParameter [] paras, CommandType ct) {int res; using (cmd = new SqlCommand (plain text, GetConn () {cmd. commandType = ct; cmd. parameters. addRange (paras); res = cmd. executeNonQuery ();} return res ;} /// <summary> /// execute the query SQL statement or stored procedure /// </summary> /// <param name = "plain text"> query the SQL statement or stored procedure </param> /// <param name = "ct"> command type </param> /// <returns> </returns> public DataTable ExecuteQuery (string plain text, commandType ct) {DataTable dt = new DataTable (); cmd = new SqlCommand (plain text, GetConn (); cmd. commandType = ct; using (sdr = cmd. executeReader (CommandBehavior. closeConnection) {dt. load (sdr) ;}return dt ;} /// <summary> /// execute the SQL statement or stored procedure with parameters. /// </summary> /// <param name = "plain text"> query the SQL statement or stored procedure </param> /// <param name = "paras"> parameter set </param> /// <param name = "ct"> command type </param >/// <returns> </returns> public DataTable ExecuteQuery (string plain text, 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 Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.