首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

DBhelper&DBhelper&DBhelper&DBhelper来看瞥这个有缺陷吗

2014-01-03 
DBhelper&DBhelper&DBhelper&DBhelper来看看这个有缺陷吗?这个DBhelper可以吗?有些没用到SqlParameter的方

DBhelper&DBhelper&DBhelper&DBhelper来看看这个有缺陷吗?
这个DBhelper可以吗?有些没用到SqlParameter的方法不安全?谁有更好一点的DBHELPER?


private static SqlConnection sc;
        public static SqlConnection Sc
        {
            get
            {
                string StrConWeb = ConfigurationManager.ConnectionStrings["YunNiao"].ConnectionString;
                try
                {
                    if (sc == null)
                    {
                        sc = new SqlConnection(StrConWeb);
                        sc.Open();
                    }
                    else if (sc.State == ConnectionState.Closed)
                    {
                        sc.Open();
                    }
                    else if (sc.State == ConnectionState.Broken)
                    {
                        sc.Close();
                        sc.Open();
                    }
                    return sc;
                }
                catch (Exception e)
                {
                    throw new Exception(e.Message);
                }
            }
        }

        public static int ExeSql(string sql)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Sc);
                int i = cmd.ExecuteNonQuery();
                return i;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        public static int ExeSql(string sql, params SqlParameter[] sp)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Sc);
                cmd.Parameters.AddRange(sp);
                return cmd.ExecuteNonQuery();
            }


            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        public static string ExScala(string sql)
        {
            try
            {

            
            SqlCommand cmd = new SqlCommand(sql, Sc);
            string  ExeScalar =cmd.ExecuteScalar().ToString();
            return ExeScalar;
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }

        public static SqlDataReader GetSelect(string sql)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql, Sc);
                SqlDataReader dr = cmd.ExecuteReader();

                return dr;
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }

        public static SqlDataReader GetReader(string sql, params SqlParameter[] para)
        {
            try
            {
                SqlCommand cmd = new SqlCommand(sql,Sc);
                cmd.Parameters.AddRange(para);
                return cmd.ExecuteReader();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

        public static DataTable GetAll(string sql)
        {
            try
            {
                SqlDataAdapter sda = new SqlDataAdapter(sql, Sc);
                DataSet ds = new DataSet();
                sda.Fill(ds);

                return ds.Tables[0];
            }
            catch (Exception e)
            {

                throw new Exception(e.Message);
            }
        }

        public static DataTable GetTable(string sql, params SqlParameter[] para)


        {
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(sql,Sc);
                da.SelectCommand.Parameters.AddRange(para);
                DataSet ds = new DataSet();
                da.Fill(ds);
                return ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }

微软官网有DBSqlHelper通用类的。。而不是什么SqlHelper
[解决办法]
微软官网有DBSqlHelper通用类的。。而不是什么SqlHelper

你在哪看见我写SqlHelper了。。。。

你的代码里面尽是 SqlConnection 和SqlCommand。一看就知道是面向MSSQL的。而不是DBCommon
[解决办法]
网上D个现成的吧,别纠结了!
[解决办法]
缺陷: 方法命名很糟糕,全是静态成员,跟sqlclient耦合
[解决办法]
还有异常处理的方式是错误的。

热点排行