How to read from ap.config

  • Thread starter Thread starter ad
  • Start date Start date
A

ad

I have stored my connectionstring in ap.config like:
<configuration>
<appSettings/>
<section name="microsoft.visualstudio.testtools"
type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection,
Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=8.0.0.0,
Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<connectionStrings>
<add name="myDataBase" connectionString="Data Source=.\;Initial
Catalog=SchoolGov;Integrated Security=True" />
</connectionStrings>
</configuration>

How can I read it into application?
 
Add your connection strings to the appSettings section rather than creating a
custom section like this:

<appSettings>
<add key="myDataBase" value="Data Source=localhost;Integrated
Security=SSPI;Initial Catalog=northwind" />
</appSettings>

Then, to get the value, use:

string connectionstring =
System.Configuration.ConfigurationSettings.AppSettings["myDataBase"];


If you are determined to use a custom section for your connection strings, I
suggest starting with this article from Chris Sells:

http://www.sellsbrothers.com/askthewonk/secure/default.aspx?content=whatsthebestwaytomaintain.htm

HTH

Dale Preston
MCAD C#
MCSE, MCDBA
 
Back
Top