在Windows窗体应用程序中处理连接字符串的最佳方法是什么?

Els*_*han 5 c# sql-server app-config

我的C#应用​​程序使用DataSetTableAdapters.它们是从VS2008 GUI工具生成的.

例:

右键单击项目 - >添加新项 - >数据集

此方法自动添加连接字符串app.config.

但这是连接字符串的硬连线方法.我想以一种简单的方式更改连接字符串.但是当我使用数据集时,连接字符串从应用程序属性获取.这种情况有什么解决方案吗?

在此输入图像描述

这是我在Settings.Designer.cs文件中的连接字符串存储

    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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

app.config包含

<?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>
Run Code Online (Sandbox Code Playgroud)

JP *_*ons 6

我相信你这样问,所以你不必在本地测试和生产/测试服务器之间手动更改.

您可能希望在部署Web应用程序项目时查看如何:转换Web.config

它是关于web.config而不是app.config,但它是相同的想法.

PS.仅适用于VS 2010及以上版本