Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

how do I access and manipulate the connectionString in app config?

I want to access the connectionString in appconfig so that I won't use the hardcoded string in my windows forms. Also, how do I manipulate the connection string? I plan to make the user of the application create a new db (Ms Access 2003) that's why the connectionString will change when that happens, right? How do I do these? Thanks in advance. Btw I'm using C# .NET Windows Forms and MS Access 2003.
0
0nepiece
Asked:
0nepiece
2 Solutions
 
Meir RivkinFull stack Software EngineerCommented:
using ConfigurationManager won't do any good cause u get ConfigurationErrorsException if u try to change setting saying: "[System.Configuration.ConfigurationErrorsException] = {"The configuration is read only."}"

u would have to handle the conection string yourself.
u can still use the app.config to store/modify connection string properties, however, u will have to create a custom configuration manager which will use other methods to access the app.config and update the connection string.
since app.config is xml based file u can parse it as one, using XElement,XmlDocument etc.
or better yet u can load the app.config explicitly using ConfigurationManager.OpenMappedExeConfiguration() and then add/modify connection string properties:


                Configuration exConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap
                {
                    ExeConfigFilename = @"c:\temp\app.config"
                }, ConfigurationUserLevel.None);
                exConfiguration.ConnectionStrings.ConnectionStrings[0] = new ConnectionStringSettings("<NAME>","<CONNECTION_STRING>","<PROVIDER>");




0
 
Meir RivkinFull stack Software EngineerCommented:
code snippet:
Configuration exConfiguration = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap
                {
                    ExeConfigFilename = @"c:\temp\app.config"
                }, ConfigurationUserLevel.None);
                exConfiguration.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings("2222","44444"));

Open in new window

0
 
0nepieceAuthor Commented:
I can't seem to find the ConfigurationManager. What is available is "System.Configuration.ConfigurationSettings" that's why I'm unable to locate OpenMappedExeConfiguration()
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
lenordisteCommented:
if you plan to have a connection string for each user and have VS2005 or VS2008 then you should consider using Application Settings since it will handle a lot of things for you: reading, saving (if using the scope "USER"), accessing a fully typed variable.

Here is a starting place :
http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx
0
 
Meir RivkinFull stack Software EngineerCommented:
@0nepiece

u need to add the System.Configuration reference
0
 
aplusexpertCommented:
Hi,


First update the App config file to which database you want to connect.

using below code:

XmlDocument _XmlDoc = new XmlDocument();
                _XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

                foreach (XmlElement _xElement in _XmlDoc.DocumentElement)
                {
                    if (_xElement.Name == "appSettings")
                    {
                        foreach (XmlNode _xNode in _xElement.ChildNodes)
                        {
                            if (_xNode.Attributes[0].Value == _psKeyName)
                            {
                                _xNode.Attributes[1].Value = _psKeyValue;
                            }
                        }
                    }
                }
                _XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);


Then after proceed as usual to connect database.

Thanks

0
 
0nepieceAuthor Commented:
Thanks for all the inputs! The configuration manager did the job!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now