码迷,mamicode.com
首页 > 数据库 > 详细

SqlHelper类

时间:2018-03-12 10:28:19      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:using   blog   const   lda   comm   command   object   gpo   取数据   

 

 public class SqlHelper
    {
        /// <summary>
        /// 获取连接字符串
        /// </summary>
        static string conStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;

        #region ExecuteNonQuery
        /// <summary>
        /// 执行非查询语句,使用sql语句与参数化查询,返回受影响行数,如果执行的是非增删改语句返回-1
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="ps"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] ps)
        {
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddRange(ps);
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion

        #region ExecuteScalar
        /// <summary>
        /// 执行查询  返回的是首行首列
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="ps">sql三参数化数组</param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] ps)
        {
            using (SqlConnection conn = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddRange(ps);
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }
        #endregion

        #region ExecuteReader
        /// <summary>
        /// 按行读取数据
        /// </summary>
        /// <param name="sql">sql语句</param>
        /// <param name="ps">参数</param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps)
        {
            SqlConnection conn = new SqlConnection(conStr);
            try
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddRange(ps);
                    conn.Open();
                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
            }
            catch (Exception ex)
            {
                conn.Dispose();
                throw ex;
            }
        }
        #endregion

        #region SqlDataAdpter
        /// <summary>
        /// 取出数据集DataSet
        /// </summary>
        /// <param name="cmdText"></param>
        /// <param name="ps"></param>
        /// <returns></returns>
        public static DataSet GetDataSet(string cmdText, params SqlParameter[] ps)
        {
            DataSet ds = new DataSet();
            using (SqlDataAdapter sda = new SqlDataAdapter(cmdText, connStr))
            {
                sda.SelectCommand.Parameters.AddRange(ps);
                sda.Fill(ds);
            }

            return ds;
        }
        #endregion


    }

 

SqlHelper类

标签:using   blog   const   lda   comm   command   object   gpo   取数据   

原文地址:https://www.cnblogs.com/Jenkin/p/8547492.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!