The examples described in this paper are different from those generated by code generators, such as SqlHelper, Codesmith, etc., which are commonly generated on the Internet. In fact, many of the sqlhelper generated by the code generator are not used in actual development, consider a beginner if the method of encapsulation class is too many, will cause certain trouble, also will give them to increase the burden, so this article lists the actual use of the four more commonly used methods, in fact, The most commonly used should be two, that is, check and delete and change, the other two are also used relatively few.
It should be explained that sqlhelper in the development of WinForm more, The encapsulation class used in asp.net and MVC projects is similar to WinForm, but there is a certain difference, because the big projects are in the kind of better framework, or their own company developed the framework, which the package is also different, this article summarizes these four methods in WinForm used more commonly.
The main code is as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Configuration;
Using System.Data;
Using System.Data.SqlClient;
namespace SQL {public static class SqlHelper {///<summary>///Create a connection string///</summary> Static readonly string connstr=configurationmanager.connectionstrings["ConnectionString"].
ConnectionString;
#region 1.0 Executes the query statement, returns a table + static DataTable excutetable (String sql, params sqlparameter[] PS)///<summary> 1.0 Execute Query statement, return a table///</summary>///<param name= "SQL" >sql statement </param>///<param name= " PS "> parameter array </param>///<returns> returns a table </returns> public static DataTable excutetable (String sql,
Params sqlparameter[] ps {SqlDataAdapter da = new SqlDataAdapter (SQL, CONNSTR); Da.
SelectCommand.Parameters.AddRange (PS);
DataTable dt = new DataTable (); Da.
Fill (DT); Return DT; #endregion #region 2.0 The method to perform additions and deletions + static int excutenoquery (String sql, params sqlparameter[] PS)///<s Ummary>///2.0 methods for implementing additions and deletions///</summary>///<param name= "SQL" >sql statements </param>///<p Aram Name= "PS" > Parameter array </param>///<returns> returns a record </returns> public static int Excutenoquery (str ing sql, params sqlparameter[] PS {using (SqlConnection conn = new SqlConnection (connstr)) {con
N.open ();
SqlCommand command = new SqlCommand (SQL, conn); Command.
Parameters.addrange (PS); return command.
ExecuteNonQuery ();
#endregion #region 3.0 method of executing stored procedures + static int Excuteproc (string procname, params sqlparameter[] PS) <summary>///3.0 methods for executing stored procedures///</summary>///<param name= "procname" > Stored procedure name </param>
; <param name= "PS" > Parameter array </param>///<returns></returns> publicstatic int Excuteproc (string procname, params sqlparameter[] PS) {using (SqlConnection conn = new SqlConnection
(CONNSTR)) {Conn.
Open ();
SqlCommand command = new SqlCommand (procname, conn);
Command.commandtype = CommandType.StoredProcedure; Command.
Parameters.addrange (PS); return command.
ExecuteNonQuery ();
#endregion #region 4.0 Query result set, return header column + static int execscalar (String sql, params sqlparameter[] PS) <summary>///4.0 Query result set, return header column///</summary>///<param name= "SQL" >sql statement </param>
; <param name= "PS" > Parameter array </param>///<returns></returns> public static Object Execscalar (
String sql, params sqlparameter[] PS//call to determine what type {using (SqlConnection conn = new SqlConnection (CONNSTR)) {Conn.
Open ();
SqlCommand command = new SqlCommand (SQL, conn); Command.
Parameters.addrange (PS); REturn command.
ExecuteScalar ();
}} #endregion}}
It is believed that this article has some reference value for the. NET program design.