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

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

问题描述

有没有办法更改数据库对象的连接字符串中的企业图书馆在运行时?我发现的链接,但它有点过时(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)

我还发现<一href="http://stackoverflow.com/questions/63546/vs2005-c-programmatically-change-connection-string-contained-in-appconfig">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.

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

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