|
-
January 28th, 2010, 04:25 PM
#1
Configuring App.Config File for Connection String
I been working on a Printer INK Management Program for my Company. We have many different districts we deal with. Each district has a different Server.
I am trying to create the application where they can place the database file anywhere they would like on the server and when they first run the application it would ask them to choose the location of where they placed the file.
I would like to know how i would be able to edit the connection string information in the app.config file during run time.
Otherwise if there is a better solution PLEASE HELP! I am in desperate need of help.
Thanks.
-
February 4th, 2010, 07:54 PM
#2
Re: Configuring App.Config File for Connection String
Here a way to write in settings of an application, if this is really what you need
NOTE:
1)will work only when exe is launced out of ide.
2)will work only if user has enough permission on filesystem
Code:
Imports System
Imports System.Configuration
Imports System.Configuration.ConfigurationSettings
Imports System.Configuration.ClientSettingsSection
Imports System.Xml
Public Class RegisterCnn
Public Shared Function GetStrConnection(ByVal cnnName As String) As String
Dim retval As String = ""
' Get the application configuration file.
Dim config As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
'when in Ide, it search for the wrong file:
'exename.svchost.exe.config
'instead of
'exename.exe.config
'this code creates that file, but ide will kill it as
'soon as debug is stopped. Thus to see this working and keep the results,
'you should launch the exe via explorer (or outside the Ide)...
Dim connections As ConnectionStringsSection = _
config.ConnectionStrings
If connections.ConnectionStrings.Count <> 0 Then
Debug.Print("Connection strings:")
' Loop to get the collection elements.
For Each connection As ConnectionStringSettings In connections.ConnectionStrings
Dim name As String = connection.Name
Dim provider As String = connection.ProviderName
Dim connectionString As String = connection.ConnectionString
Debug.Print("Name: {0}", name)
Debug.Print("Connection string: {0}", connectionString)
Debug.Print("Provider: {0}", provider)
If name.ToLower = cnnName.ToLower Then
retval = connectionString
Exit For
End If
Next
Else
'No connection string is defined. ask for one...
retval = "No CNN"
End If
'debug:
'Dim xmlRd As New System.Xml.XmlDocument()
'If System.IO.File.Exists(config.FilePath) Then
' xmlRd.Load(config.FilePath)
'End If
Return retval
End Function
Public Shared Function WriteStrConnection(ByVal cnnName As String, ByVal cnnString As String) As Boolean
'get the file
Dim config As System.Configuration.Configuration = _
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim Cnn As New ConnectionStringSettings(cnnName, cnnString)
If config.ConnectionStrings.ConnectionStrings.Item(cnnName) Is Nothing Then
config.ConnectionStrings.ConnectionStrings.Add(Cnn)
Else
config.ConnectionStrings.ConnectionStrings.Item(cnnName).ConnectionString = cnnString
End If
config.Save(ConfigurationSaveMode.Modified)
''debug
'Dim xmlRd As New System.Xml.XmlDocument()
'xmlRd.Load(config.FilePath)
End Function
End Class
Last edited by Cimperiali; February 15th, 2010 at 05:02 AM.
...at present time, using mainly Net 4.0, Vs 2010
Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
all the other wonderful people who made and make Codeguru a great place.
Come back soon, you Gurus.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|