C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ADO.NET »

How To Get Connection String


Posted Date: 18 Jun 2008    Resource Type: Code Snippets    Category: ADO.NET
Author: RohitMember Level: Gold    
Rating: 1 out of 5Points: 10



Suppose you have the config settings as
<configuration>
<connectionStrings>
<add name="Name"
providerName="System.Data.SqlClient"
connectionString ="data source=localhost;Integrated Security=SSPI; Initial Catalog=UR_DB_NAME"/>
</connectionStrings>
</configuration>

There are two ways to get the connection string that is stored in your config file.

[B] 1. By Name [/B]


string strConnectionString = string.Empty;
ConnectionStringSettings objSettings = ConfigurationManager.ConnectionStrings["Name"];
if (objSettings != null)
{
strConnectionString = objSettings.ConnectionString;
}
SqlConnection m_objConnection = new SqlConnection(strConnectionString);


[B] 2. By Provide Name [/B]

string strConnectionString = string.Empty;
ConnectionStringSettings objSettings = ConfigurationManager.ConnectionStrings["System.Data.SqlClient"];
if (objSettings != null)
{
strConnectionString = objSettings.ConnectionString;
}
SqlConnection m_objConnection = new SqlConnection(strConnectionString);




Responses

Author: Sneha Singla    18 Jun 2008Member Level: Silver   Points : 1
That is a very handy tip to get the connection strings. I have faced many issue due to this. Thanks for sharing your knowledge.


Author: bharat kumar    22 Jun 2009Member Level: Gold   Points : 1
string conn = ConfigurationManager.ConnectionStrings["your connection string name"].ConnectionString;


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Read connection string from web.config  .  Connection string  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Where to Store Database Connection
Previous Resource: ExecuteReader Implementation
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
Related Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use