C# 简单的SQLHelper

 public class SQLHelper
    {
        private string m_SqlConnectionString = "";
        private SqlConnection conn = null;

        public SQLHelper(string sqlConnectionString)
        {
            m_SqlConnectionString = sqlConnectionString;
            if (!TestSqlConnection()) throw new Exception("数据库连接失败");
        }

        private SqlConnection GetConn()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            return conn;
        }
        private bool TestSqlConnection()
        {
            bool IsCanConnect = true;
            try
            {
                conn = new SqlConnection(m_SqlConnectionString);
                conn.Open();
            }
            catch
            {
                IsCanConnect = false;
            }
            finally
            {
                conn.Close();
            }
            return IsCanConnect;
        }

        /// <summary>
        /// 执行增删改操作
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <returns></returns>
        public int ExecuteNoQuery(string cmdText, CommandType cmdType)
        {
            int ret = 0;
            try
            {
                using (SqlCommand cmd = new SqlCommand(cmdText, GetConn()))
                {
                    cmd.CommandType = cmdType;
                    ret = cmd.ExecuteNonQuery();
                }
                return ret;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }
        /// <summary>
        /// 执行查询操作
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <returns></returns>
        public DataTable ExecuteQuery2DataTable(string cmdText, CommandType cmdType)
        {
            DataTable dt = null;
            try
            {
                using (SqlCommand cmd = new SqlCommand(cmdText, GetConn()))
                {
                    cmd.CommandType = cmdType;
                    using (SqlDataReader sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        dt = new DataTable();
                        dt.Load(sdr);
                    }
                }
                return dt;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }
        /// <summary>
        /// 执行查询操作
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="cmdType"></param>
        /// <returns></returns>
        public DataSet ExecuteQuery2DataSet(string cmdText)
        {
            DataSet ds = null;
            try
            {
                using (SqlDataAdapter da = new SqlDataAdapter(cmdText, GetConn()))
                {
                    ds = new DataSet();
                    da.Fill(ds);
                }
                return ds;
            }
            catch
            {
                conn.Close();
                throw;
            }
        }
    }

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值