DAL类SQLCOMMONS代码:using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace DAL
{
    /// <summary>
    /// SQL公共連接類,用于所有的數據庫連接
    /// </summary>
    class SqlCommons
    {
        /// <summary>
        /// 構造函數
        /// </summary>
        public SqlCommons()
        {
        }
        private string _SqlString=null;
        /// <summary>
        /// SQL連接字符串
        /// </summary>
        public string SqlString
        {
            set { _SqlString = "Data Source=VGA-PCN;Initial Catalog=BiosCopyRoom;User ID=bios;PWD=bios"; }
            get { return _SqlString; }
        }
        private SqlConnection _SqlConn=null;
        /// <summary>
        /// 打開的SQL連接
        /// </summary>
        public SqlConnection SqlConn
        {
            set { _SqlConn = GetSqlConn(); }
            get { return _SqlConn; }
        }
        public  SqlConnection GetSqlConn()
        {
            SqlConnection NewSqlConn = new SqlConnection(SqlString);
            NewSqlConn.Open();
            return NewSqlConn;
        }
        /// <summary>
        /// 關閉打開的SQL連接
        /// </summary>
        public void Close()
        {
            if (SqlConn.State == ConnectionState.Open)
            {
                SqlConn.Close();
            }
        }
    }
}DAL类中的操作代码:using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Data;namespace DAL
{
    /// <summary>
    /// 登錄系統驗證底層類
    /// </summary>
    public class DALLogon
    {
        /// <summary>
        /// 構造函數
        /// </summary>
        public DALLogon()
        {        }
        /// <summary>
        /// 執行存儲過程
        /// </summary>
        /// <param name="SPName">存儲過程名</param>
        /// <param name="Param">存儲過程參數組</param>
        public void ExcuteSP(string SPName, SqlParameter[] Param)
        {
            SqlCommons SqlConn = new SqlCommons();
            SqlCommand SqlComm = new SqlCommand(SPName, SqlConn.SqlConn);//这里老是报错connection没有初始化
            SqlComm.CommandType = CommandType.StoredProcedure;
            SqlComm.Parameters.AddRange(Param); 
            SqlComm.ExecuteNonQuery();
            SqlConn.Close();
        }
    }
}

解决方案 »

  1.   

            private string _SqlString=null;
            /// <summary>
            /// SQL連接字符串
            /// </summary>
            public string SqlString
            {
                set { _SqlString = "Data Source=VGA-PCN;Initial Catalog=BiosCopyRoom;User ID=bios;PWD=bios"; }//自己看这句有什么问题...
                get { return _SqlString; }//如果调用者没有为SqlString赋值则永远为null...
            }这段代码是很可笑的,就算你写对了硬编码ConnectionString也是很可笑的...你不知道怎么用配置文件吗?分层不用配置文件分来做什么?时髦吗?
      

  2.   


            public SqlConnection SqlConn
            {
                set { _SqlConn = GetSqlConn(); }
                get { return _SqlConn; }
            }LZ 都没有 set 直接 get _SqlConn 为null 当然出错啦 
    直接        public SqlConnection SqlConn
            {
                get { return GetSqlConn(); }
            }
    就好了  当然这样做不太好
      

  3.   

    是用web.config文件中的 connectionstring吗?
    我新手 
      

  4.   

    学到用存储过程没啊!用下面的代码:
    int number;
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    SqlCommand objCommand = new SqlCommand("SQL中存储过程", conn);                objCommand.CommandType = CommandType.StoredProcedure;                objCommand.Parameters.Add("@LoginID(SQL中的字段)", SqlDbType.NVarChar, 50).Value = admin.LoginId;                objCommand.Parameters.Add("@LoginPwd", SqlDbType.NVarChar, 50).Value = admin.LoginPwd;                objCommand.Parameters.Add("@AdminName", SqlDbType.NVarChar, 50).Value = admin.AdminName;                objCommand.Parameters.Add("@Sex", SqlDbType.NVarChar, 50).Value = admin.Sex;
                    conn.Open();
                    number = Convert.ToInt32(objCommand.ExecuteScalar());
                    conn.Close();
                    conn.Dispose();
                }
                return number;
      

  5.   

    SqlCommand SqlComm = new SqlCommand(SPName, SqlConn.GetSqlConn());
    看看petshop中sqlhelp
      

  6.   

    /// <summary>
            /// 構造函數
            /// </summary>
            public SqlCommons()
            {
            }
    在上面的 构造方法里加上了 _SqlConn = GetSqlConn();
    另外下面的属性下的非常不好!写在set里还不如写的构造方法里呢!
    public SqlConnection SqlConn
            {
                set { _SqlConn = GetSqlConn(); }
                get { return _SqlConn; }
            }
      

  7.   


     public void ExcuteSP(string SPName, SqlParameter[] Param)
            {
                SqlCommons SqlConn = new SqlCommons();
                SqlCommand SqlComm = new SqlCommand(SPName, SqlConn.Open());//这里应该是。。
                SqlComm.CommandType = CommandType.StoredProcedure;
                SqlComm.Parameters.AddRange(Param); 
                SqlComm.ExecuteNonQuery();
                SqlConn.Close();
            }低级错误!!
      

  8.   

    配置文件不好用还是怎么了?http://msdn.microsoft.com/zh-cn/downloads/system.configuration.aspxhttp://msdn.microsoft.com/zh-cn/downloads/system.configuration.configurationsettings.aspx去看看``