Global ConnectionString未初始化

时间:2011-03-22 02:42:57

标签: c# asp.net connection-string data-access-layer

我的连接字符串存储在myGlobals.cs页面中,如下所示:

/* Connection String */
public static string conString
{
    get { return _conString; }
    set { _conString = ConfigurationManager.ConnectionStrings["BaseConnectionString"].ToString(); }
}

我在4层架构中设置了代码。所以我有一个Business Object文件夹,Business Access Layer&数据访问层。如果我将连接字符串移动到DAL结构中,它可以正常工作。其他明智的我得到这个错误:

The ConnectionString property has not been initialized

myGlobals.cs类是否在此之前未被包含或是否需要更改?

这是我的数据访问层:

public DataTable Load()
    {
        SqlConnection conn = new SqlConnection(MyGlobals.conString);
        SqlDataAdapter dAd = new SqlDataAdapter("administratorGetAll", conn);
        dAd.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataSet dSet = new DataSet();
        try
        {
            dAd.Fill(dSet, "AdministratorsTable");
            return dSet.Tables["AdministratorsTable"];
        }
        catch
        {
            throw;
        }
        finally
        {
            dSet.Dispose();
            dAd.Dispose();
            conn.Close();
            conn.Dispose();
        }
    }

1 个答案:

答案 0 :(得分:3)

属性'setter'不会自动调用;当您将属性放在作业的左侧时,会执行其代码 - MyClass.MyProperty = "new value";

你想这样做:

public static string conString 
{     
    get { return ConfigurationManager.ConnectionStrings["BaseConnectionString"]; }
}