2

Possible Duplicate:
VS2005 C# Programmatically change connection string contained in app.config

I have created a WinForms project in my PC using C#, and would like to deploy the project to other pc's, but I need to change the connection string in the app.config in order to connect the sql sever database in other pc's, and I want the users to configure the connection string.

How can I accomplish this?

Community
  • 1
  • 1
user1286824
  • 21
  • 1
  • 3
  • Is there a pattern to the different databases on the machines ? – Micah Armantrout Mar 30 '12 at 17:31
  • The database is sqlserver 2008 express edition, and will be running in a thin client server, and i want to deploy my written application to the server, so each thin client can use the application which interacts withe database, but i want to have the users to configure the connection between the application and the database – user1286824 Mar 30 '12 at 17:38

4 Answers4

5

If you've created a settings file for your project, there will be a class that represents the properties in a type-safe manner. This is typically something like TiddleBits.Properties.Settings.Default, where TwiddleBits is your assembly name.

If you haven't created a settings file for your project, do it. It vastly simplifies working with configuration files.

Once the class exists, you can read and write properties just like any other class. Just remember to save your changes once you've finished assigning properties, or they'll be discarded.

Mike Hofer
  • 16,477
  • 11
  • 74
  • 110
3

Like others have said creating your own config file is the way to go. Here is some code that may help

Just create an app.config file in VS and put it in the same directory as dll. It will look something like this.

    <?xml version="1.0" encoding="utf-8" ?>
      <configuration>
       <appSettings>
        <add key="YourThing" value="Something" />
       </appSettings>
    </configuration>

Then you can load it lke this.

string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

        string configFile = System.IO.Path.Combine(appPath, "App.config");
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
        configFileMap.ExeConfigFilename = configFile;

        System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

You can then read values like this.

config.AppSettings.Settings["YourThing"].Value;

And Save Values like this.

config.AppSettings.Settings["YourThing"].Value = "New Value";
config.Save();
Jive Boogie
  • 1,265
  • 1
  • 12
  • 20
2

You can tell the users to edit the app.config, or you can provide a UI and use the ConfigurationManager to edit the app.config at runtime.

Edit app config at runtime

Update app.config system.net setting at runtime

Community
  • 1
  • 1
Rob Rodi
  • 3,476
  • 20
  • 19
1

Modify the app config from code. http://chiragrdarji.wordpress.com/2008/09/25/how-to-change-appconfig-file-run-time-using-c/

wavesmash
  • 21
  • 1