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

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

问题描述

有没有办法在运行时更改Enterprise Library中DataBase对象的连接字符串?我找到了链接,但它有点过时2005)



我还发现,但它似乎适用于.Net一般来说,我想知道是否有一些可以专门为EntLib做。



我只是传递连接字符串名称到DatabaseFactory对象中的CreateDatabase()方法,并且昨天工作,我的项目经理要求我支持多个数据库实例。它发生,我们必须有一个数据库每个状态(一个用于CA,一个FL,等...)所以我的软件需要循环遍历所有的数据库,做一些数据,但它会使用相同的配置文件。 p>

提前感谢。

解决方案

a href =http://msdn.microsoft.com/en-us/library/dd139953.aspx =nofollow noreferrer> Enterprise Library文档 - 添加应用代码
它说:


如果你知道
的连接字符串你要创建的数据库,你
可以绕过应用程序的
配置信息,并使用
构造函数直接创建
Database对象。因为Database
类是一个抽象基类,所以
必须构造一个派生的
类型派生的数据库类型
确定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.

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

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