1

I am trying to read a connection string from the application configuration file.

But keep receiving an error:

The name ConfigurationManager does in exit in current context.

After googling the error, I added ConfigurationManager to my project. But I still get the same error.

My code:

string sqlConStr = ConfigurationManager.ConnectionStrings["AppConnectionString"].ToString();`    

My application config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="AppConnectionString" connectionString="SERVER=1894; Database=db; UID=loss;  PWD=where;encrypt=no;enlist=false" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>
4

2 に答える 2

2

を参照していることを確認してくださいSystem.configuration

次に、正しい名前空間をインポートするようにしてください

using System.Configuration;

または完全なタイプ名を使用します

string sqlConStr = System.Configuration.ConfigurationManager.ConnectionStrings["AppConnectionString"].ToString();
于 2012-07-24T12:03:09.113 に答える
0

使ってみてくださいConnectionString

var connection = ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString;
于 2012-07-24T12:02:44.340 に答える