connectionstring属性尚未初始化 - AppSettings

时间:2017-03-22 19:42:34

标签: c# asp.net .net connection-string appsettings

下面是我的连接字符串,我不知道我在哪里收到错误。

<appSettings>

<!-- Default database to use... -->
<add key="Database_default" value="DB_Servername"/>

<!-- Datamart database connection to use -->
<add key="Database_datamart" value="MED_PROD"/>

<!--  Development Datamart Database Server -->
<add key="MED_PROD" value="Initial Catalog=REPORT_MED;Data Source=DB_Servername;UID=app_UID;PWD=******;"/>

<!--  Dev Server - Port Reference-->
<add key="va3fin01" value="Initial Catalog=acc;Data Source=DB_Servername;UID=med_UID;PWD=*******;"/>

<!-- MEDFIN_DEV -->
<add key="MED_DEV" value="Initial Catalog=MED_DEV;Data Source=DB_Servername;UID=med_UID;PWD=********;"/>

 </appSettings>

我无法更改任何后端代码。他们让我改变服务器。更改服务器后,我得到&#34; connectionstring属性尚未初始化&#34;在一个工作的应用程序如果有任何我只能在web.config

上处理,请告诉我

请帮忙! 感谢

被修改

以下是用于获取连接字符串的方法。

private void GetConnectionString() 
    {
        string _settingname;

        // if its empty, update it with "default"
        if (this._database.Trim().Length == 0) this._database = "default";

        // get the setting...
        _settingname = ConfigurationSettings.AppSettings["Database_" + this._database];
        if (_settingname == null) 
        {
            throw new Exception("Unable to determine connection settings for specified database.");
        } 
        else
        {
            // retrieve the connection string from the specified database...
            this._ConnectionString = ConfigurationSettings.AppSettings[_settingname];
        }
    }

1 个答案:

答案 0 :(得分:0)

通常,连接字符串位于 web.config 内的 connectionstring 标记内。

例如,

<connectionStrings>
   <add name="MED_PROD" connectionString="Initial Catalog=REPORT_MED;Data Source=DB_Servername;UID=app_UID;PWD=******;" providerName="System.Data.SqlClient"/>
   <add name="va3fin01" connectionString="Initial Catalog=acc;Data Source=DB_Servername;UID=med_UID;PWD=*******;" providerName="System.Data.SqlClient"/>
   <add name="MED_DEV" connectionString="Initial Catalog=MED_DEV;Data Source=DB_Servername;UID=med_UID;PWD=********;" providerName="System.Data.SqlClient"/>
</connectionStrings>