根据构建配置更改Windows窗体中的连接字符串?

时间:2012-12-12 17:23:07

标签: visual-studio-2010 .net-4.0 xdt-transform

类似于如何在ASP.NET中拥有多个Web.config,是否可以拥有多个App.config,具体取决于构建配置?

我想更改连接字符串,因此如果我正在构建配置test,则连接字符串应为:

"Server=test;Initial Catalog=test..."

如果我针对test2构建,则连接字符串将为:

"Server=anotherserver; Initial Catalog=test2..."

目前,我是手动完成的,所以我想知道如何实现自动化?

2 个答案:

答案 0 :(得分:1)

虽然它们不是本机支持,但您也可以为Windows Forms项目(App.config)进行XML转换,而不仅仅是Web应用程序项目。

有一个VS插件可以启用支持,虽然我从来没有尝试过,但我会推荐你​​安装了VS的环境,因为至少有一位我确信拥有深厚知识的微软员工在MSBuild上,因为他已经创作了book on the subject

SlowCheetah - XML Transforms

我不知道SlowCheetah是否为未安装VS的CI服务器提供支持,因此我还会根据自定义目标文件的内容提出另一种解决方案。

Visual Studio App.config XML Transformation

(无耻的插件通知,我是上面那个的原作者)

答案 1 :(得分:0)

我们在这里使用似乎运作良好的策略。我们每个解决方案都有一个Web.config。我认为这也适用于app.configs,但从未尝试过。您创建一个类,称之为例如ConfigurationManager。在课堂上应该去这个代码:

public static class ConfigurationManager
{
    private static NameValueCollection _appSettings = new NameValueCollection();
    private static ConnectionStringSettingsCollection _connSettings = new ConnectionStringSettingsCollection();

    private enum SettingsType {App, Connection};

    public static NameValueCollection AppSettings
    {
        get 
        {
            if (_appSettings.Count == 0)
            {
                LoadEnvironmentSettings(SettingsType.App);
            }
            return _appSettings;

        }

    }

    public static ConnectionStringSettingsCollection ConnectionStrings
    {
        get
        {
            if (_connSettings.Count == 0)
            {
                LoadEnvironmentSettings(SettingsType.Connection);
            }
            return _connSettings;
        }
    }

    private static void LoadEnvironmentSettings(SettingsType settingsType)
    {
        string strEnvironment = string.Empty, strSeparator = ".";
        if (ConfigurationManager.AppSettings.HasKeys())
        {
            strEnvironment = ConfigurationManager.AppSettings["Environment"];

            if (!String.IsNullOrEmpty(strEnvironment))
            {
                strEnvironment += strSeparator;
                switch (settingsType)
                {
                    case SettingsType.App:
                        LoadAppSettings(strEnvironment.ToUpper());
                        break;
                    case SettingsType.Connection:
                        LoadConnectionSettings(strEnvironment.ToUpper());
                        break;
                }
            }
            else //that part should never happen
            {
                throw new Exception("Missing Environment AppSetting");
            }
        }
    }
    private static void LoadAppSettings(string environmentType)
    {
        string appKey = string.Empty;
        foreach (string key in ConfigurationManager.AppSettings.AllKeys)
        {
            if (key.StartsWith(environmentType))
            {
                appKey = key.Replace(environmentType, string.Empty);
                _appSettings.Add(appKey, ConfigurationManager.AppSettings[key]);
            }
        }
    }
    private static void LoadConnectionSettings(string environmentType)
    {
        //ConnectionStringSettings connStrings = new ConnectionStringSettings();
        string connKey = string.Empty;

        foreach (ConnectionStringSettings str in ConfigurationManager.ConnectionStrings)
        {
            if (str.Name.StartsWith(environmentType))
            {
                connKey = str.Name.Replace(environmentType, string.Empty);
                _connSettings.Add(new ConnectionStringSettings(connKey, str.ConnectionString));
            }
        }
    }
}

您需要添加System,System.Collections.Generic,System.Collections.Specialized,System.Configuration和System.Text。确保您的类与您正在处理的项目位于相同的命名空间中。然后,在配置文件中添加两个(或更多)键,例如,

<add name="TEST.MyConnectionString" connectionString="Data Source=TESTServer;Initial Catalog=MyDatabase;user id=MyUser;pwd=Mypwd" />
<add name="TEST2.MyConnectionString" connectionString="Data Source=TEST2Server;Initial Catalog=MyDatabase;user id=MyUser;pwd=Mypwd" />

最后,在您的machine.config中,在appSettings中添加以下内容:

<add key="Environment" value="TEST"/>

<add key="Environment" value="TEST2"/>

取决于您想要做什么。我们通常放置DEV,STAGING和PROD键,然后在我们的三个不同的DEV,STAGING和PROD服务器上有不同的machine.config文件。希望这有帮助