2

我正在寻找一种在运行时配置数据库连接的方法;专门使用企业库。我看到有一个 *.Data.Configuration (或接近这个的东西......不记得我的头顶)组装但在互联网上发现的不多。使问题复杂化的是 API 帮助在 Vista 上被破坏的事实。

现在,我发现了这个解决方法:

Configuration cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConnectionStringSettings connection = new ConnectionStringSettings();
connection.Name = "Runtime Connection";
connection.ProviderName = "System.Data.OleDb";
connection.ConnectionString = "myconstring";
cfg.ConnectionStrings.ConnectionStrings.Add(connection);
cfg.Save(ConfigurationSaveMode.Modified);

ConfigurationManager.RefreshSection("connectionStrings");
var runtimeCon = DatabaseFactory.CreateDatabase("Runtime Connection");

虽然它给了我想要的东西,但它会永久编辑 App.config。当然我可以回去删除更改,但我不想经历这个麻烦。

4

3 回答 3

1

如果您使用的是 winforms 应用程序,您可以尝试使用UserProperties来存储此信息。另一种可能的解决方案是自定义配置部分

于 2008-08-19T04:35:14.750 回答
0

Nope, you must save in order for the EntLib (and, I suspect, any other tool) to see the changes.

于 2008-08-19T15:13:48.313 回答
0

如果您不想保存它,则不需要执行 cfg.Save 命令。

配置对象将存储您的更改,直到不再需要它为止。

于 2008-08-19T04:41:27.997 回答