SoFunction
Updated on 2025-03-06

Simple analysis of the usage of encapsulating CURD to SqlHelper class implemented in C#

This article describes the C# implementation of encapsulation of CURD to SqlHelper class. Share it for your reference, as follows:

1. Let me briefly explain that in general, the database connection string is configured in a file and then referenced in the code. So, let's take a look at the file here.

First look at what needs to be added:

Parameter description:

name: represents a key value. In the code, you need to use this key value to find the corresponding connection string information.

connectionString: String information that links the database. Server: Database: Database, UID: Database Account Name, PWD: Database Password

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
 <!--This writes the database link configuration information,Add it manually,Strictly case sensitive-->
 <connectionStrings>
  <add name="mssql" connectionString="Server=.;Database=db_test;uid=sa;pwd=123456"/>
 </connectionStrings>
 <!--This writes the database link configuration information,Add it manually,Strictly case sensitive-->
</configuration>

2. Next is the code. The introduction is in the comments, just look at the code.

using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace SQLHelper
{
  /// <summary>
  /// For the sake of convenience of calling, the SqlHelper class is generally written as a static class  /// </summary>
  public static class SqlHelper
  {
    //References need to be added--framework, add and use    private static readonly string conStr = ["mssql"].ConnectionString;
    /// <summary>
    /// Encapsulate the method adding, deleting, and modifying methods. The method name can be written at will, but for the sake of convenience, the same method name as the SqlCommand class is used.    /// </summary>
    /// <param name="sql">SQL statement</param>    /// <param name="pms">SQL parameters, because I don't know how many parameters there will be, I use variable parameter params</param>    /// <returns>Number of affected rows</returns>    public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
    {
      using (SqlConnection conn = new SqlConnection(conStr))
      {
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
          if (pms != null)
          {
            (pms);
          }
          ();
          return ();
        }
      }
    }
    /// &lt;summary&gt;
    /// Query a single result, generally used with the aggregate function    /// &lt;/summary&gt;
    /// <param name="sql">Query SQL statement</param>    /// <param name="pms">SQL parameters</param>    /// <returns>Return the query object, the first row and first column of the query result</returns>    public static object ExecuteScalar(string sql, params SqlParameter[] pms)
    {
      using (SqlConnection conn = new SqlConnection(conStr))
      {
        using (SqlCommand cmd = new SqlCommand(sql, conn))
        {
          if (pms != null)
          {
            (pms);
          }
          ();
          return ();
        }
      }
    }
    /// &lt;summary&gt;
    /// Query multiple lines    /// &lt;/summary&gt;
    /// <param name="sql">SQL statement</param>    /// <param name="pms">SQL parameters</param>    /// <returns>Returns SqlDataReader object</returns>    public static SqlDataReader ExcuteReader(string sql, params SqlParameter[] pms)
    {
      //Using cannot be used here, otherwise an error will be reported when returning to SqlDataReader, because it has been closed in using when returning.      //In fact, in using database related classes, SqlConnection must be closed, but other options can be closed because CG recycles are automatically recycled      SqlConnection conn = new SqlConnection(conStr);
      using (SqlCommand cmd = new SqlCommand(sql, conn))
      {
        if (pms != null)
        {
          (pms);
        }
        try
        {
          ();
          //The enumeration is passed in order to enable the corresponding SqlConnection after using SqlDataReader outside.          return ();
        }
        catch
        {
          ();
          ();
          throw;
        }
      }
    }
  }
}

For more information about C# related content, please check out the topic of this site:Summary of common database operation techniques in C#》、《Tutorial on the usage of common C# controls》、《Summary of C# form operation skills》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.