您可以在App.config
文件中包含连接字符串。在应用程序部署过程中,您可以通过查询registery
来检查用户机器是否安装了SQL Server。基于这个结果更新应用程序配置与数据库特定的连接字符串。在app.config
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="DbConnectionString" value="Will be updated during deployment" />
</appSettings>
</configuration>
更新连接字符串:
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);
把它放在'app.config'文件。 –
它是一个Windows服务? – VahidNaderi
把连接字符串放在'app.config'文件中 – paul