What's Here?
- Members: 131,931
- Replies: 470,198
- Topics: 72,877
- Snippets: 2,538
- Tutorials: 665
- Total Online: 1,727
- Members: 74
- Guests: 1,653
Who's Online?
|
I use this function in my applications to retrieve the connection string from the app.config file. It is especially handy in the application I'm working on now as there are 2 databases it can connect to.
|
Submitted By: PsychoCoder
|
|
Rating:
 
|
|
Views: 10,302 |
Language: VB.NET
|
|
Last Modified: August 2, 2007 |
Instructions: Store your connection stirng in the app.config like this:
< connectionStrings >
< add name="YourName"
connectionString="Persist Security Info=False;Data Source=Database_Name;Initial Catalog=Table_Name;Integrated Security=SSPI;Trusted_Connection=TRUE;Application Name=Application_Name"
providerName="System.Data.SqlClient" />
< /connectionStrings >
To use:
Dim SqlConnection As New SqlConnection("GetConnectionString("YourName")) |
Snippet
Public Shared Function GetConnectionString(ByVal strConnection As String) As String
'Declare a string to hold the connection string
Dim sReturn As New String("")
'Check to see if they provided a connection string name
If Not String.IsNullOrEmpty(strConnection) Then
'Retrieve the connection string fromt he app.config
sReturn = ConfigurationManager. & _
ConnectionStrings(strConnection).ConnectionString
Else
'Since they didnt provide the name of the connection string
'just grab the default on from app.config
sReturn = ConfigurationManager. & _
ConnectionStrings("YourConnectionString").ConnectionString
End If
'Return the connection string to the calling method
Return sReturn
End Function
Copy & Paste
|
|
|
Reference Sheets
Bye Bye Ads
Free DIC T-Shirt
Related Sites
Monthly Drawing
Partners
Top Contributors
Top 10 Kudos This Month
|