在企业库中在运行时更改连接字符串 [英] Changing connection string at runtime in Enterprise Library

查看:40
本文介绍了在企业库中在运行时更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在运行时更改企业库中数据库对象的连接字符串?我找到了 this 链接,但它有点过时了(2005)

我也发现了这个但是它似乎一般适用于 .Net,我想知道是否可以专门为 EntLib 做一些事情.

我只是将连接字符串名称传递给 DatabaseFactory 对象中的 CreateDatabase() 方法,直到昨天我的项目经理要求我支持多个数据库实例时,它才起作用.碰巧我们每个州必须有一个数据库(一个用于 CA,一个用于 FL,等等),所以我的软件需要遍历所有数据库并对数据执行某些操作,但它将使用相同的配置文件.

提前致谢.

解决方案

如果你看看 "企业库文档 - 添加应用程序代码"它是这样说的:

<块引用>

"如果你知道连接字符串你要创建的数据库,你可以绕过应用程序的配置信息并使用构造函数直接创建数据库对象.因为数据库类是一个抽象基类,你必须构造其派生之一类型.派生的数据库类型确定 ADO.NET 数据提供程序.例如,SqlDatabase 类使用 SqlClientFactory 提供程序,SqlCeDatabase 类使用SqlCeProviderFactory 提供程序,以及OracleDatabase 类使用OracleClientFactory 提供程序.它是您有责任构建适当类型的数据库类连接字符串."

然后继续举一些例子.这表明您不应该使用 DatabaseFactory,而应该为每个不同的连接创建一个新的 Database 类.

Is there a way to change the connection string of a DataBase object in Enterprise Library at runtime? I've found this link but its a little bit outdated (2005)

I've also found this but it seems to apply to .Net in general, I was wondering if there was something that could be done specifically for EntLib.

I was just passing the connection string name to the CreateDatabase() method in DatabaseFactory object and that worked til yesterday that my project manager asked me to support more than one database instance. It happens that we have to have one database per state (one for CA, one for FL, etc...) so my software needs to cycle through all databases and do something with data but it will use the same config file.

Thanks in advance.

解决方案

If you take a look at "Enterprise Library Docs - Adding Application Code" it says this:

"If you know the connection string for the database you want to create, you can bypass the application's configuration information and use a constructor to directly create the Database object. Because the Database class is an abstract base class, you must construct one of its derived types. The derived Database type determines the ADO.NET data provider. For example, the SqlDatabase class uses the SqlClientFactory provider, the SqlCeDatabase class uses the SqlCeProviderFactory provider, and the OracleDatabase class uses the OracleClientFactory provider. It is your responsibility to construct the appropriate type of Database class for the connection string."

It then goes on to give some examples. This would suggest that you should not be using the DatabaseFactory and you should be creating a new Database class for each of your different connections.

这篇关于在企业库中在运行时更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆