What's Here?
- Members: 98,765
- Replies: 387,053
- Topics: 58,106
- Snippets: 2,026
- Tutorials: 546
- Total Online: 1,043
- Members: 40
- Guests: 1,003
Who's Online?
|
This is a snippet I use to retrieve my database connection string I store in the web.config file.
|
Submitted By: PsychoCoder
|
|
Rating:
 
|
|
Views: 6,674 |
Language: C#
|
|
Last Modified: January 20, 2008 |
Instructions: Pass the method and it will return the connection string for that name (works well if you have multiple connections in the config file) and it will retrieve that specific connection string.
If you fail to provide the name of a connection string it will default to your default (usually 1st in the list) to the default string. |
Snippet
#region GetConnectionstring
/// <summary>
/// method to retrieve connection stringed in the web.config file
/// </summary>
/// <param name="str">Name of the connection</param>
/// <remarks>Need a reference to the System.Configuration Namespace</remarks>
/// <returns></returns>
public string GetConnectionString(string str)
{
//variable to hold our return value
string conn = string.Empty;
//check if a value was provided
if (!string.IsNullOrEmpty(str))
{
//name provided so search for that connection
conn = ConfigurationManager.ConnectionStrings[str].ConnectionString;
}
else
//name not provided, get the 'default' connection
{
conn = ConfigurationManager.ConnectionStrings["YourConnName"].ConnectionString;
}
//return the value
return conn;
}
#endregion
Copy & Paste
|
|
|
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|