Ken Barrett

Ken Barrett

  • NA
  • 123
  • 0

retrieving the connection string from the app.config file

Jul 31 2009 4:22 PM
I want to retrieve the DB connection string from the app.config file.
Here is my app.config file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="dbConnection"
            connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\App_Data\HREChk.mdf;Integrated Security=True;User Instance=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Question 1 ) How do I pull this out? I want dbConnection connectionString. This ought to be simple, but the document is driving me nuts.

Question 2 ) You will note that part of the path for the connectionString contains "|DataDirectory|" (obviously a variable, maybe and ENV variable), where is this stored and how is it expanded?

TIA Ken

ETA: edited in Firefox.

Answers (4)