Article From:https://www.cnblogs.com/sansui/p/9967403.html
using System;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntiSlipSys
{
    public class DBHelper
    {
        //Database Connection and Addition, Deletion and Change Check
        static String conn = "Data Source=.;Initial Catalog=AntiSlipSys;Integrated Security=True";//Modify the connection string according to the project

        SqlConnection comm = new SqlConnection(conn);
        SqlCommand command = null;
        SqlDataReader reader = null;
        DataSet ds = null;
        SqlDataAdapter adapter = null;
        
        #region Open database connectionpublic void OpenConnection()
        {
            try
            {
                comm.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        #endregion

        #region Close database connectionpublic void CloseConnection()
        {
            try
            {
                if (reader != null)
                {
                    reader.Close();
                }
                if (comm != null)
                {
                    comm.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        #endregion

        #region Data query/// <summary>
        /// Linked query/// </summary>
        /// <param name="sql">Query SQL statement</param>
        /// <returns>DataReader object returned by query</returns>
        public SqlDataReader Query(string sql)
        {
            this.OpenConnection();
            command = new SqlCommand(sql,comm);
            reader = command.ExecuteReader();
            return reader;
        }

        #endregion

        #region Data update operation/// <summary>
        /// User Performs Data Update I: Update D: Delete U: Modify/// </summary>
        /// <param name="sql">Execute add-delete SQL statements</param>
        /// <returns>Returns the number of rows affected in the database</returns>
        public int NoQuery(string sql)
        {
            this.OpenConnection();
            command = new SqlCommand(sql,comm);
            int result = command.ExecuteNonQuery();
            this.CloseConnection();
            return result;
        }

        #endregion

        #region Query a single value [aggregation function]/// <summary>
        /// Users execute data queries for individual values [aggregation functions]/// </summary>
        /// <param name="sql">SQL statement to execute queries</param>
        /// <returns>Returns a single value</returns>
        public int Aggre(string sql)
        {
            this.OpenConnection();
            command = new SqlCommand(sql, comm);
            int result = (int)command.ExecuteScalar();
            this.CloseConnection();
            return result;
        }

        #endregion

        #region Break open query/// <summary>
        /// Break open query/// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public DataSet GetList(string sql)
        {
            ds = new DataSet();
            adapter = new SqlDataAdapter(sql,comm);
            adapter.Fill(ds,"Info");
            return ds;
        }

        #endregion

    }
}

Complete DBHelper database operation base class

Leave a Reply

Your email address will not be published. Required fields are marked *