Four commonly used instances of.NET's SQLHELPER methods

  • 2021-01-18 06:23:15
  • OfStack

The examples described in this article are different from sqlhelper generated by code generators common on the Internet, such as software, CodeSmith, etc. Actually generate many sqlhelper code generator are made with less than in the actual development, consider if the novice wrapper class methods too much, will cause the 1 set, also can give their burden, so in this paper, the practice again is to list the four methods which are frequently used, in fact, the most commonly used is supposed to be two, is to check and deleted, the other two are used less.

To be sure, sqlhelper in winform development, with more in asp. net and mvc items used in the wrapper class with winform similar, but there are 1 set of distinction, because big projects are in a better frame, or your company development, the framework of the wrapper class is also different, this article summarizes the four methods are frequently used in winform by.

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 string for the connection 
    /// </summary>
    static readonly string connStr=ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

 #region 1.0  Execute the query and return 1 A table  + static DataTable ExcuteTable(string sql, params SqlParameter[] ps)
    /// <summary>
    /// 1.0  Execute the query and return 1 A table 
    /// </summary>
    /// <param name="sql">sql statements </param>
    /// <param name="ps"> Parameters of the array </param>
    /// <returns> return 1 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  Methods of implementing additions, deletions and modifications  + static int ExcuteNoQuery(string sql, params SqlParameter[] ps)
    /// <summary>
    /// 2.0  Methods of implementing additions, deletions and modifications 
    /// </summary>
    /// <param name="sql">sql statements </param>
    /// <param name="ps"> Parameters of the array </param>
    /// <returns> return 1 records </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  Method to execute a stored procedure  + static int ExcuteProc(string procName, params SqlParameter[] ps)
    /// <summary>
    /// 3.0  Method to execute a stored procedure 
    /// </summary>
    /// <param name="procName"> Stored procedure name </param>
    /// <param name="ps"> Parameters of the 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 the first row, first column  + static int ExecScalar(string sql, params SqlParameter[] ps)
    /// <summary>
    /// 4.0  Query result set, return the first row, first column 
    /// </summary>
    /// <param name="sql">sql statements </param>
    /// <param name="ps"> Parameters of the array </param>
    /// <returns></returns>
    public static object ExecScalar(string sql, params SqlParameter[] ps) // What type is it when you call it 
    {
      using (SqlConnection conn = new SqlConnection(connStr))
      {
        conn.Open();
        SqlCommand command = new SqlCommand(sql, conn);
        command.Parameters.AddRange(ps);
        return command.ExecuteScalar();
      }
    } 
    #endregion
  }
}

I believe that this article described to everyone.net program design has a certain reference value.


Related articles: