Getting the connectionstring in .net 4
I want to get a开发者_高级运维 connection string from the app.config
file.
connectionString = System.Configuration.ConfigurationSettings.AppSettings["DBEntities"];
But it doesn't work. It's empty.
I cannot access the System.Configuration.ConfigurationManager
because it's .NET 4.
How can I get my connection string from the app.config
?
Use
string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
Make sure to add reference to System.configuration
in your project.
In .net 4 you have to use:
ConfigurationManager.ConnectionStrings["name of connection string in web.config"]
More about it is here and here.
Add a reference to System.Configuration to your project and use ConnectionStrings instead of AppSettings
精彩评论