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

Ensure you have referenced System.configuration.

Then make sure you either import the correct namespace

using System.Configuration;

or use the full typename

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

Try using ConnectionString

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