Stuff that's in my head

Can open... Worms everywhere! The blog of Colin Angus Mackay
posts - 270, comments - 232, trackbacks - 38

Tip of the Day #4 (Connection Strings in Config files)

From .NET 2.0 onwards a new and improved configuration management system has been put in place. You can now add a <connectionString> element to the config file and use it to place the connection strings to the database and then retrieve then in a consistent way in your application. It supports multiple connection strings too if you need to access multiple databases.

The config file looks like this:

<configuration>
...
<connectionStrings>
<
add name="Default" connectionString="Server=(local);database=MyDatabase"/>
</
connectionStrings>
...
<configuration>

From the .NET application you can access the connection string like this:

connectionString = 
    ConfigurationManager.ConnectionStrings["Default"].ConnectionString;

Just remember to add a reference to System.Configuration in your project and ensure that the code file is using the System.Configuration namespace as well.

Print | posted on Saturday, July 19, 2008 12:51 PM

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   

Powered by: