The SqlHelper class

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    //In a static class, when calling this method is not used in the creation of a new object
    public static class SqlHelper
    {
        private static readonly string constr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
        /// <summary>
        /// Implementation of Insert, delete, Update returns the number of rows affected
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType">Type in the SqlCommand command (a stored procedure, T— Sql statements, etc.)</param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, CommandType cmdType, params SqlParameter[] pms)
        {
            using (SqlConnection con=new SqlConnection(constr))
            {                
                using (SqlCommand cmd=new SqlCommand(sql,con))
                {                    
                    cmd.CommandType = cmdType;
                    if (pms!=null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        /// <summary>
        /// Returns the query in the first line of the first
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType">Type in the SqlCommand command (a stored procedure, T— Sql statements, etc.)</param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, CommandType cmdType, params SqlParameter[] pms)
        {
            using (SqlConnection con=new SqlConnection(constr))
            {
                using (SqlCommand cmd=new SqlCommand(sql,con))
                {
                    cmd.CommandType = cmdType;
                    if (pms!=null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }
        /// <summary>
        /// The query returns a select statement is executed data
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="cmdType">Type in the SqlCommand command (a stored procedure, T— Sql statements, etc.)</param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuDataReader(string sql, CommandType cmdType, params SqlParameter[] pms)
        {
            //Because ExecuteReader is a line of reading, so must ensure that con is connected, so there can not use using automatic release resources
            SqlConnection con = new SqlConnection(constr);
            try
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    cmd.CommandType = cmdType;
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteReader(CommandBehavior.CloseConnection);//When the ExecuteReader method when an exception occurs, the CommandBehavior.CloseConnection will close the connection
                }
            }
            catch (Exception)
            {
                if (con!=null)
                {
                    con.Close();
                    con.Dispose();                   
                }
                 throw;
            }
            
        }
        public static DataTable ExecuteDataTable(string sql, CommandType cmdType, params SqlParameter[] pms)
        {
            SqlDataAdapter da = new SqlDataAdapter(sql,constr);
            da.SelectCommand.CommandType = cmdType;
            if (pms!=null)
            {
                da.SelectCommand.Parameters.AddRange(pms);
            }
            DataTable dt = new DataTable();
            da.Fill(dt);
            return dt;
        }
    }
}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Becky at November 17, 2013 - 11:45 AM