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

sqlhelper

时间:2015-06-07 15:43:26      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

class SqlHelper
    {
        private static string connStr = ConfigurationManager.ConnectionStrings["dbconstr"].ConnectionString;
        //public static int ExecuteNonQuery(string sql)
        //{
        //    using(SqlConnection conn=new SqlConnection(connStr))
        //    {
        //        conn.Open();
        //        using (SqlCommand cmd = conn.CreateCommand())
        //        {
        //            cmd.CommandText = sql;
        //            return cmd.ExecuteNonQuery();
        //        }
        //    }
        //}
        //public static object ExecuteScalar(string sql)
        //{
        //    using(SqlConnection conn=new SqlConnection(connStr))
        //    {
        //        conn.Open();
        //        using(SqlCommand cmd=conn.CreateCommand())
        //        {
        //            cmd.CommandText = sql;
        //            return cmd.ExecuteScalar();
        //        }
        //    }
        //}
        //public static DataTable ExecuteDataSet(string sql)
        //{
        //    using (SqlConnection conn = new SqlConnection(connStr))
        //    {
        //        conn.Open();
        //        using(SqlCommand cmd=conn.CreateCommand())
        //        {
        //            cmd.CommandText = sql;
        //            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        //            DataSet dataset = new DataSet();
        //            adapter.Fill(dataset);
        //            return dataset.Tables[0];
        //        }
        //    }
        //}
        public static int ExecuteNonQuery(string sql,SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    //foreach (SqlParameter param in parameters)
                    //{
                    //    cmd.Parameters.Add(param); 
                    //}
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteNonQuery();
                }
            }
        }
        public static object ExecuteScalar(string sql,SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();
                }
            }
        }
        //public static DataTable ExecuteDataSet(string sql,SqlParameter[] parameters)
        //{
        //    using (SqlConnection conn = new SqlConnection(connStr))
        //    {
        //        conn.Open();
        //        using (SqlCommand cmd = conn.CreateCommand())
        //        {
        //            cmd.CommandText = sql;
        //            cmd.Parameters.AddRange(parameters);
        //            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
        //            DataSet dataset = new DataSet();
        //            adapter.Fill(dataset);
        //            return dataset.Tables[0];
        //        }
        //    }
        //}
        public static DataTable ExecuteDataSet(string sql, params SqlParameter[] parameters)//params长度可变参数 必须是数组
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataset = new DataSet();
                    adapter.Fill(dataset);
                    return dataset.Tables[0];
                }
            }
        }
    }

 

sqlhelper

标签:

原文地址:http://www.cnblogs.com/lushixiong/p/4558468.html

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