ado.net SqlHelp类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace Reservation.DAL
{
    public class SqlHelper
    {
        /// 
        /// 自定义数据库连接字符串
        /// 
        public static string ConnString = @"Data Source=.;Initial Catalog=DB_Date_Plan; User ID=sa;Pwd=111111";
        /// 
        /// 应用程序下获取连接字符串
        /// 
        //public static string ConnString
        //{
        //    get {

        //        return System.Configuration.ConfigurationManager.ConnectionStrings["connstring"].ToString();
        //    }
        //}

        /// 
        /// 执行一个Sql语句返回受影响的行数
        /// 
        /// insert,update,delete或相关的存储过程
        /// 命令类型:SQL语句还是存储过程
        /// SQL语句所需要的参数
        public static int ExcuteSQLReturnInt(string sql, CommandType type, params SqlParameter[] pars)
        {
            //定义连接对象
            SqlConnection conn = new SqlConnection(ConnString);
            //判断连接对象的状态,并且打开
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            try
            {
                //实例化命令对象
                SqlCommand cmd = new SqlCommand(sql, conn);
                //判断CommandType类型是否是SQL语句还是存储过程
                cmd.CommandType = type;
                if (pars != null && pars.Length > 0)
                {
                    foreach (SqlParameter p in pars)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
                //调用方法执行SQL语句或存储过程
                int count = cmd.ExecuteNonQuery();
                return count;
            }
            catch (Exception ex)
            {
                return 0;
            }
            finally
            {
                //记得要关闭连接
                conn.Close();
            }
        }

        /// 
        /// 执行一个Sql语句或存储过程,返回一条记录,sqldataReader
        /// 
        /// select语句,或相关的存储过程
        /// 指定命令类型,sql语句还是存储过程,默认的是sql语句
        /// 参数的集合
        /// 
        public static SqlDataReader ExcuteSqlReturnReader(string sql, CommandType type, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand(sql, conn);
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter p in pars)
                {
                    cmd.Parameters.Add(p);
                }
            }
            cmd.CommandType = type;
            //当reader.close(),也就是关闭了datareader时,CommandBehavior.CloseConnection 也会关闭连接!
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        /// 
        /// 执行一个sql语句或存储过程,不带参数的!返回一天记录 sqldataReader
        /// 
        /// 
        /// 
        /// 
        public static SqlDataReader ExcuteSqlReturnReader(string sql, CommandType type)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandType = type;
            //当reader.close(),也就是关闭了datareader时,CommandBehavior.CloseConnection 也会关闭连接!
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            return reader;
        }

        /// 
        ///  执行一个Sql语句或存储过程,返回一个数据集,dataset
        /// 
        /// select语句或相关的存储过程
        /// 命令类型
        /// 命令类型
        /// DataSet
        public static DataSet SelectSqlReturnDataset(string sql, CommandType type, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            //数据集识别器对象SqlDataAdapter 会自动打开数据库链接!
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter p in pars)
                {
                    sda.SelectCommand.Parameters.Add(p);
                }
            }
            sda.SelectCommand.CommandType = type;
            DataSet ds = new DataSet();
            sda.Fill(ds);
            return ds;
        }

        /// 
        /// 执行一个sql语句返回一个数据表对象
        /// 
        /// select
        /// 命令类型
        /// 参数集合
        /// DataTable
        public static DataTable SelectSqlReturnDataTable(string sql, CommandType type, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            if (pars != null && pars.Length > 0)
            {
                foreach (SqlParameter p in pars)
                {
                    sda.SelectCommand.Parameters.Add(p);
                }
            }
            sda.SelectCommand.CommandType = type;
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

        //*******************************************************************
        //SelectSqlReturnDataTable方法的重载,不传参数的情况下
        /// 
        /// 执行一个sql语句返回一个数据表对象,不传参数
        /// 
        /// select
        /// 命令类型
        /// 
        public static DataTable SelectSqlReturnDataTable(string sql, CommandType type)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            sda.SelectCommand.CommandType = type;
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }

        /// 
        /// 执行一个sql语句或相关的存储过程,返回一个值
        /// 
        /// select count(*) from tablename where ....
        /// 命令类型
        /// 所需的参数
        /// object
        public static object selectSqlReturnObject(string sql, CommandType type, SqlParameter[] pars)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = type;
                if (pars != null && pars.Length > 0)
                {
                    foreach (SqlParameter p in pars)
                    {
                        cmd.Parameters.Add(p);
                    }
                }
                object obj = cmd.ExecuteScalar();
                return obj;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

        /// 
        /// 执行一个sql语句或相关的存储过程,返回一个值,不传参数
        /// 
        /// 
        /// 
        /// 
        public static object selectSqlReturnObject(string sql, CommandType type)
        {
            SqlConnection conn = new SqlConnection(ConnString);
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Open();
            }
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = type;
                object obj = cmd.ExecuteScalar();
                return obj;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

    }
}

 

转载于:https://www.cnblogs.com/youguess/p/7130240.html

你可能感兴趣的:(ado.net SqlHelp类)