Four common ones. Net SqlHelper Method instances

Source: Internet
Author: User
The examples described in this paper are different from those generated by code generators, such as SqlHelper, Codesmith and so on. In fact, the code generator generated SqlHelper Many of the methods in the actual development are not used, considering beginners if the package class method too much, will cause a certain amount of trouble, will give them increased burden, so this article lists the actual use of the four more commonly used methods, in fact, The most commonly used should be two, is to check and delete and change, the other two is also used relatively few.

It is necessary to note that SqlHelper in the development of WinForm used more, The encapsulation classes used in ASP. NET and MVC are similar to WinForm, but there are some differences, because large projects are used in a better framework, or the framework of their own company development, and the package classes are also different, this article summarizes the four methods used in WinForm.

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 connected string///</summary> static readonly string Connstr=configurationmanager.con nectionstrings["ConnectionString"].  ConnectionString; #region 1.0 executes a query statement that 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" &G  t; 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 ways to perform additions and deletions + static int excutenoquery (String sql, params sqlparameter[] PS)///<summary>//2.0 execution How to change///</summary>//<param name= "SQL" >sql statement </param>//<param name= "PS" > parameter array </p  aram>//<returns> Returns a record </returns> public static int excutenoquery (String sql, params sqlparameter[] PS) {using (SqlConnection conn = new SqlConnection (CONNSTR)) {Conn.        Open ();        SqlCommand command = new SqlCommand (SQL, conn); Command.        Parameters.addrange (PS); return command.      ExecuteNonQuery (); }} #endregion #region 3.0 methods 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> public static 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 first row column + static int execscalar (String sql, params sqlparameter[] PS)// <summary>///4.0 query result set, return first column///</summary>//<param name= "SQL" >sql statement </param>// /<param Name= "PS" > Parameter array </param>//<returns></returns> public static object Execscalar (Stri         ng 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}}

The

believes that this article has certain reference value to everyone's. NET programming.

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.