Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best method to handle connection string in windows form application?

My C# application use DataSet and TableAdapters. They are generated form VS2008 GUI Tool.

Example:

Right click project - > Add New Item - > DataSet

This method add a connection string automatically into app.config.

But this is hard corded method for connection string. I want to change the connection string in a easy way. But when I used data set, then connection string get from application property. Are there any solution for this situation?

enter image description here

This is my connection string store in Settings.Designer.cs file

    namespace WindowsFormsApplication2.Properties {


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        [global::System.Configuration.DefaultSettingValueAttribute("Data Source=SLCERT\\SQLEMK;Initial Catalog=TestDataBase;Integrated Security=True")]
        public string TestDataBaseConnectionString {
            get {
                return ((string)(this["TestDataBaseConnectionString"])); // this is the connection string get from the dataset's
            }
        }
    }
}

app.config contains

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WindowsFormsApplication2.Properties.Settings.TestDataBaseConnectionString"
            connectionString="Data Source=SLCERT\SQLEMK;Initial Catalog=TestDataBase;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>
like image 914
Elshan Avatar asked Mar 25 '13 08:03

Elshan


1 Answers

I believe that you ask this, so that you don't have to manually change between local testing and production/test server.

You might want to look at How to: Transform Web.config When Deploying a Web Application Project

it's about web.config instead of app.config but it is the same idea.

ps. only for VS 2010 and above

like image 89
JP Hellemons Avatar answered Sep 27 '22 18:09

JP Hellemons