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

C# 在windows窗体应用程序中处理连接字符串的最佳方法是什么?,c#,sql-server,app-config,C#,Sql Server,App Config,我的C#应用程序使用数据集和表格适配器。它们是从VS2008 GUI工具生成的 例如: 右键单击项目->添加新项->数据集 此方法会自动将连接字符串添加到app.config中 但这是连接字符串的硬记录方法。我想以一种简单的方式更改连接字符串。但当我使用数据集时,连接字符串来自应用程序属性。 这种情况有什么解决办法吗 这是我的连接字符串存储在Settings.Designer.cs文件中 namespace WindowsFormsApplication2.Properties {

我的C#应用程序使用
数据集
表格适配器
。它们是从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
            }
        }
    }
}
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>

我相信您会问这个问题,这样您就不必在本地测试和生产/测试服务器之间手动切换

你可能想看看

它是关于web.config而不是app.config的,但想法是一样的


注:仅适用于VS 2010及以上版本

除了JP Hellemons提供的进行配置转换的建议之外,您还可以做一些其他事情,因为.NET 4.0仅支持(本机支持)。您可以向配置文件的
部分添加任意数量的连接字符串,因此可以添加“DebugConnectionString”和“ReleaseConnectionString”或类似内容

现在,为了在每次环境更改时不进行干预地使用这些,您可以使用跟踪常量。假设在Visual Studio中的local上,您正在使用
DEBUG
常量集进行编译,并且在为发行版部署时,该常量不存在,那么您可以执行以下操作:

#if DEBUG 
    return ConfigurationManager.ConnectionStrings["DebugConnectionString"];
#else
    return ConfigurationManager.ConnectionStrings["ReleaseConnectionString"];
#endif
在app.config中

<configuration>
<configSections>
</configSections>
<connectionStrings>
    <add name="DBCS" connectionString="Data Source=|DataDirectory|\Database.sdf;password=Password" 
        providerName="Microsoft.SqlServerCe.Client.3.5" /> 
</connectionStrings>
包括

System.Configuration;

名称空间

应用程序做什么并不容易?有什么比编辑app.config更容易?app.config伴随着应用程序。它是xml。在任何文本编辑器中都可以轻松编辑!我这么问。但现在的web应用程序是Windows窗体应用程序:)那好吧。更多信息请点击此处:或者点击此处查看general propose。但是,当我使用DataSet(DataSet使用GUI工具时,VS会通过设置文件自动添加连接字符串。我想替换该部分)时,如果您想使用app.config中的连接字符串。创建一个类从数据库获取数据将其存储在类中并调用该类来填充控件,如datagridview。此方法可以,但当我使用DataSet时,我希望在其发布之前每次都重新配置。(DataSet由vs2008 GUI工具生成,然后它会自动将连接字符串添加到settings.cs文件中)
System.Configuration;