新手的我,写的一个数据库类(单例模式),大家来找错,谢谢。
-------------------------------------------------
这个类代码不全是我的,是网上下的然后修改的,但是我修改成“单例模式”了,我不知代码有没有不对的地方,谢谢。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;namespace gongjijin
{
    class clsdb
    {
        private static clsdb theSingleton = null;   //单例模式对象
        public SqlConnection conn = null;           //连接对象
        private SqlDataReader dataReader = null;    //dataRader  对象
        private SqlCommand command = null;          //命令对象
       
        /// <summary>
        /// 构造函数
        /// </summary>
        private clsdb()
        {           
            //连接数据库字符串
            string connStr = "Data source=.;Initial Catalog=db1;User id=sa;pwd=0000";
            conn = new SqlConnection(connStr);
            //打开数据库连接
            conn.Open();
            command = new SqlCommand();
            command.Connection = conn;
        }        //实例方法
        public static clsdb Instance()
        {
            if (null == theSingleton)
            {
                theSingleton = new clsdb();
            }
            return theSingleton;
        }        /// <summary>
        /// 增删改查
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int UpdateDeleteAdd(string sql)
        {
            command.CommandText = sql;
            int rows = command.ExecuteNonQuery();
            return rows;
        }        /// <summary>
        /// 查询多个值的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public SqlDataReader SelectAll(string sql)
        {
            command.CommandText = sql;
            dataReader = command.ExecuteReader();
            return dataReader;
        }        /// <summary>
        /// 查询单个值的方法
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public double SelectOne(string sql)
        {
            double count;
            command.CommandText = sql;
            if (command.ExecuteScalar() is DBNull)
            {
                count = 0;                
            }
            else
            {
                count = Convert.ToDouble(command.ExecuteScalar());               
            }
            return count;
        }        /// <summary>
        /// 关闭数据库连接 
        /// </summary>
        public void GetClose()
        {
            conn.Close();
        }
    }
}

解决方案 »

  1.   

    SqlHelper
    大量使用实例的时候,使用singletone
    否则静态变量public   static   Singleton   GetInstance()   
                {   
                            if   (instance==null)   
                            {   
                                        lock   (padlock)   
                                        { 
                                                    if   (instance==null)   
                                                    instance   =   new   Singleton();   
                                        }   
                            }   
                            return   instance; 
                }   
      

  2.   

    SqlHelper
    大量使用实例的时候,使用singletone
    否则静态变量public static Singleton GetInstance()   
      {   
      if (instance==null)   
      {   
      lock (padlock)   
      {  
      if (instance==null)   
      instance = new Singleton();   
      }   
      }   
      return instance;  
      }   
      

  3.   

    没什么问题,这是最普遍的用法,平常我也是这样写的。但我看过Robert C.Martion的一篇文章,他说这样的话会“多次使用的if进行判断,做成不必要的开销”
     
    可以用一个静态变量来代替IF
    private static Clsdb cladb1=new Clsdb();
    public static clsdb Instance()
    {
          return cladb1;
    }
      

  4.   

    如果为了混一个“知道模式”的考试结果,这样写无所谓。如果你真的懂.net,要知道sqlConnection是专门设计来使用连接池的,用不着画蛇添足搞什么对象共享。而你的所谓单利,不但彻底毁掉了连接池的能力,而且自然在多用户时直接造成各种崩溃性异常。随便搜了一个文章你可以做参考:http://blog.163.com/henan_lujun/blog/static/19538333200781715121731/
      

  5.   

    应该把sqlconnection 做成单利。