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

时间:2021-12-11 16:57:30

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

我的C#应用​​程序使用DataSet和TableAdapters。它们是从VS2008 GUI Tool生成的。

Example:

Right click project - > Add New Item - > DataSet

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

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

此方法自动将连接字符串添加到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?

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

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

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

这是我在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 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>

3 个解决方案

#1


6  

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

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

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

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

ps. only for VS 2010 and above

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

#2


4  

Other than the suggestion provided by JP Hellemons to do configuration transformations, there is something else you can do, as that is only supported (natively) by .NET 4.0. You can add any number of connection strings to the <connectionStrings/> section of a configuration file, so add a "DebugConnectionString" and a "ReleaseConnectionString", or similar.

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

Now, in order to use these without intervention each time environment is changed you can use trace constants. Say on local in Visual Studio you're compiling with the DEBUG constant set and when deploying for release it's not present, then you can do something like the following:

现在,为了在没有干预的情况下使用这些,每次更改环境时都可以使用跟踪常量。在Visual Studio中使用DEBUG常量集进行编译,在部署发布时它不存在,那么您可以执行以下操作:

#if DEBUG 
    return ConfigurationManager.ConnectionStrings["DebugConnectionString"];
#else
    return ConfigurationManager.ConnectionStrings["ReleaseConnectionString"];
#endif

#3


2  

in app.config

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

and access it like this

并像这样访问它

static string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString.ToString();

include

System.Configuration;

namespace

#1


6  

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

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

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

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

ps. only for VS 2010 and above

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

#2


4  

Other than the suggestion provided by JP Hellemons to do configuration transformations, there is something else you can do, as that is only supported (natively) by .NET 4.0. You can add any number of connection strings to the <connectionStrings/> section of a configuration file, so add a "DebugConnectionString" and a "ReleaseConnectionString", or similar.

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

Now, in order to use these without intervention each time environment is changed you can use trace constants. Say on local in Visual Studio you're compiling with the DEBUG constant set and when deploying for release it's not present, then you can do something like the following:

现在,为了在没有干预的情况下使用这些,每次更改环境时都可以使用跟踪常量。在Visual Studio中使用DEBUG常量集进行编译,在部署发布时它不存在,那么您可以执行以下操作:

#if DEBUG 
    return ConfigurationManager.ConnectionStrings["DebugConnectionString"];
#else
    return ConfigurationManager.ConnectionStrings["ReleaseConnectionString"];
#endif

#3


2  

in app.config

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

and access it like this

并像这样访问它

static string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString.ToString();

include

System.Configuration;

namespace