SqlHelper 增删改查

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
    public class SQLHelper
    {
       
        private SqlConnection conn =null;
        private SqlCommand cmd = null;
        private SqlDataReader sdr = null;

        public SQLHelper()
        {
            string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
            conn = new SqlConnection(connStr);
        }

        private SqlConnection GetConn()
        {
            if (conn.State==ConnectionState.Closed)
            {
                conn.Open();
            }
            return conn;
        }

        /// <summary>
        /// 执行不带参数的增删改查SQL语句或存储过程
        /// </summary>
        /// <param name="cmdText">不带参数的增删改查SQL语句或存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public int ExecuteNonQuery(string cmdText,CommandType ct)
        {
            int res;
            using (cmd = new SqlCommand(cmdText, GetConn()))
            {
                cmd.CommandType = ct;
                res = cmd.ExecuteNonQuery();
            }
            return res;
        }

        /// <summary>
        /// 执行带参数的SQL增删改查或存储过程
        /// </summary>
        /// <param name="cmdText">参数的SQL增删改查或存储过程</param>
        /// <param name="paras">参数集合</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public int ExecuteNonQuery(string cmdText,SqlParameter[] paras,CommandType ct)
        {
            int res;
            using (cmd = new SqlCommand(cmdText, GetConn()))
            {
                
                cmd.Parameters.AddRange(paras);
                cmd.CommandType = ct;
                res = cmd.ExecuteNonQuery();
            }
            return res;
        }
        
        /// <summary>
        /// 执行带参数的增删改SQL语句或存储过程
        /// </summary>
        /// <param name="sql">带参数的增删改SQL语句或存储过程</param>
        /// <param name="ct">命令类型</param>
        /// <returns></returns>
        public DataTable ExecuteQuery(string cmdText,CommandType ct)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdText, GetConn());
            cmd.CommandType = ct;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }
        
       /// <summary>
        /// 执行带参数的SQL查询语句
       /// </summary>
        /// <param name="cmdText">带参数的SQL查询语句</param>
        /// <param name="paras">参数集合</param>
       /// <param name="ct">执行类型</param>
        /// <returns>DataTable</returns>
        public DataTable ExecuteQuery(string cmdText, SqlParameter[] paras,CommandType ct)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdText, GetConn());
            cmd.Parameters.AddRange(paras);
            cmd.CommandType = ct;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }

        /// <summary>
        /// ExecuteScalar--返回首行首列 
        /// </summary>
        /// <param name="XSqlString">sql语句</param>
        /// <returns>返回结果集的首行首列</returns>
        public int GetRecordCount(string XSqlString)
        {
            string SCount;

            SqlCommand Cmd = new SqlCommand(XSqlString, GetConn());
            SCount = Cmd.ExecuteScalar().ToString().Trim(); //返回结果集的首行首列,忽略其它
            if (SCount == "")
                SCount = "0";

            return Convert.ToInt32(SCount);
        }


    }
}

  

posted @ 2016-01-23 22:20  Akgu  阅读(282)  评论(0编辑  收藏  举报