1

我正在构建一个 .NET 4.0 C# 应用程序,它连接到 Web 服务,提取一些数据,然后将其插入数据库。该应用程序被设计为不需要用户输入,因为它将被安排为每天运行。

当我将此应用程序部署给我们的客户时,我遇到了一个问题。他们中的一些人使用 SQL Server 作为后端,一些人使用 MS Access。我能想出的唯一解决方案是将连接字符串作为命令行参数传递,但我不喜欢这种方法。由于我必须将它部署到 70 多个不同的客户,我宁愿不为每个客户编译该程序的副本。

任何想法和想法都会受到赞赏。

4

1 回答 1

3

您可以在App.config文件中包含连接字符串。在应用部署过程中,可以通过查询来检查用户机器是否安装了SQL Server registery。基于此结果使用数据库特定的连接字符串更新应用程序配置。

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>

<appSettings>
<add key="DbConnectionString" value="Will be updated during deployment" />
</appSettings>

</configuration>

更新连接字符串app.config

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings["DbConnectionString"].Value = "DB Specific Connection string";

//Save only the modified section of the config
configuration.Save(ConfigurationSaveMode.Modified);

//Refresh the appSettings section to reflect updated configurations
ConfigurationManager.RefreshSection(Constants.AppSettingsNode);
于 2013-09-16T13:33:33.423 回答