• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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