更改连接字符串在运行时企业库字符串、企业库

2023-09-02 10:49:53 作者:阳光①夏づ

有没有办法更改数据库对象的连接字符串中的企业图书馆在运行时?我发现这的链接,但它有点过时(2005)

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)

我还发现this但它似乎可以应用于.NET在一般情况下,我在想,如果有什么事,可以专门为EntLib来完成。

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.

我只是路过的连接字符串名称到的CreateDatabase()方法DatabaseFactory对象和工作直到昨天,我的项目经理要求我支持多个数据库实例。它发生,我们必须有每个国家(一个是加利福尼亚州,一为FL,等...),一个数据库,所以我的软件需要遍历所有数据库,并做一些数据,但它会使用相同的配置文件。

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.

在此先感谢。

推荐答案

如果你看一看的企业库文件 - 添加应用程序code  它说的:

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

如果你知道的连接字符串   你想要的数据库中创建,你   可以绕过应用程序的   配置信息和使用一个   构造函数直接创建   数据库对象。因为数据库   类是一个抽象基类,   必须构建其衍生的一种   类型。派生的数据库类型   决定了ADO.NET数据提供程序。   例如,SqlDatabase类   使用SqlClientFactory供应商,   在SqlCeDatabase类使用   SqlCeProviderFactory提供商,并且   Oracle数据库类使用   OracleClientFactory提供商。它是   您有责任建设   适当类型的数据库类   连接字符串。

"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."

据然后继续举一些例子。这就意味着,你不应该使用DatabaseFactory,你应该创建一个新的数据库类为每个不同的连接。

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.