SqlHelper类

Posted jenkin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SqlHelper类相关的知识,希望对你有一定的参考价值。

 

 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类的主要内容,如果未能解决你的问题,请参考以下文章

SQLHelper类

SQLHelper类

SQLHelper类

SqlHelper封装类测试

SqlHelper类系列一

SqlHelper封装类测试