Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a connectionstring in .net 4.5

How do I read a value from the web.config file for a connectionstring in the 4.5 .net framework?

I'm using system.configuration which I have a reference to and a using statement, but the only thing coming up after that to use is .ConfigurationSettings.Appsettings.

No connectionstringsettings.

like image 419
sagesky36 Avatar asked Oct 03 '12 15:10

sagesky36


1 Answers

using System.Configuration;

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

Edit (Adding in a reference to System.Configuration:

Right click on your project -> Add Reference -> Select the .NET Tab -> Select System.Configuration -> OK

http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

like image 62
Darren Avatar answered Nov 15 '22 17:11

Darren