c#封装 SqlHelper

   public static class SqlHelper
    {
        //获取配置文件中的连接字符串
        private static readonly string constr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;

        /// <summary>
        /// 执行insert、delete、update的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    //如果pms为null,则直接调用cmd.Parameters.AddRange(pms)会报错。
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteNonQuery();
                }

            }
        }

        /// <summary>
        /// 执行sql语句,返回单个值。
        /// </summary>
        /// <param name="sql">要执行的sql语句</param>
        /// <param name="pms">sql语句中的参数</param>
        /// <returns></returns>
        public static object ExecuteScalar(string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    //如果pms为null,则直接调用cmd.Parameters.AddRange(pms)会报错。
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteScalar();
                }

            }
        }

        /// <summary>
        /// 执行sql语句返回一个DataReader
        /// 当返回DataReader的时候,注意:
        /// 1.Connection不能关闭
        /// 2.DataReader不能关闭
        /// 3.command对象执行ExecuteReader()的时候需要传递一个参数CommandBehavior.CloseConnection
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] pms)
        {
            SqlConnection con = new SqlConnection(constr);
            try
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    //当调用ExecuteReader()方法的时候,如果传递一个CommandBehavior.CloseConnection参数,则表示将来当用户关闭reader的时候,系统会自动将Connection也关闭掉。
                    SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    return reader;
                }
            }
            catch          //当出现异常时需要把连接关闭
            {
                if (con != null)
                {
                    con.close();
                    con.dispose();
                }
                throw;
            }

        }

        /// <summary>
        /// 封装一个返回DataTable的方法。
        /// </summary>
        /// <param name="sql"></param>
        /// <param name="pms"></param>
        /// <returns></returns>
        public static DataTable ExecuteDataTable(string sql, params SqlParameter[] pms)
        {
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(sql, constr);
            if (pms != null)
            {
                sqlAdapter.SelectCommand.Parameters.AddRange(pms);
            }
            DataTable dt = new DataTable();
            sqlAdapter.Fill(dt);
            return dt;
        }
    }

你可能感兴趣的:(c#封装 SqlHelper)