0

我想将连接字符串作为键。所以我在 web.config 中写了这段代码。

    <add key="connectstring" value="Data Source=USER-PC;Initial Catalog=DBName; Integrated Security=False; providerName=System.Data.SqlClient"/>
  </appSettings>

但有错误,它说

Keyword not supported: 'providername'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Keyword not supported: 'providername'.

Source Error:

Line 28:             SqlConnection conn;
Line 29:             conn = new SqlConnection(ConfigurationManager.AppSettings["connectstring"].ToString());
Line 30:                 
Line 31: 

我该如何解决?

4

2 回答 2

1

尝试使用 ConfigurationManager 对象:

string connectionString 
    = ConfigurationManager.ConnectionStrings["Conn"].ConnectionString;

<connectionStrings>
    <remove name="LocalSqlServer" />
    <add name="LocalSqlServer" 
        connectionString="Initial Catalog=MyDB;Data Source=MyPC;Integrated Security=SSPI;"
            providerName="System.Data.SqlClient" />
<connectionStrings>

要使用 appSettings:

System.Configuration.ConfigurationManager.Appsettings.Get("connectstring");
于 2012-11-06T00:05:26.310 回答
1
<appSettings>
<add key="connectstring" value="Data Source=USER-PC;Initial Catalog=DBName; Integrated Security=False;" providerName="System.Data.SqlClient"/>

在您的值中单独提供 providerName

注意:您的值有错误,请在False 后添加双引号;

于 2012-11-06T00:53:03.193 回答