是否可以在另一个连接字符串中引用一个连接字符串? [英] Is it possible to reference a connection string in another connection string?
问题描述
我们有一个带有多个DLL的项目。在每个DLL中,我们都连接到一个数据库-客户端总是一个数据库。
We have a project with multiple DLLs. In each DLL, we connect to a database - always the same one for a client.
结果,我们现在有了3个几乎相同的连接字符串:一个用于我们的网站,一个用于ado.net,另一个用于telerik报告:
As a result, we now have 3 near-identical connection strings : one for our web site, one for ado.net, and one for telerik reporting :
<add name="BDConnectionString" connectionString="Data Source=localhost;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USER;Password=PASSWORD; MultipleActiveResultSets=True;Pooling=True;Max Pool Size=500;" providerName="System.Data.SqlClient" />
<add name="CMS.Reporting.My.MySettings.BDConnectionString" connectionString="Data Source=localhost;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USER;Password=PASSWORD" providerName="System.Data.SqlClient" />
<add name="KOPWebEntities" connectionString="metadata=res://*/Data.web.csdl|res://*/Data.web.ssdl|res://*/Data.web.msl;provider=System.Data.SqlClient;provider connection string="Data Source=localhost;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USER;Password=PASSWORD;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
现在它仍然可管理,但是将来,我们将拥有更多的ADO.Net Entity Framework连接,因此,客户端可能有10个连接字符串?
Now it's still manageable, but in the future, we will have more ADO.Net Entity Framework connections, so maybe 10 connection strings by client ?
有没有办法说好的,对于此连接字符串,请使用来自此处的值而不是重复它?至少对于ADO.Net吗?
Is there a way to say, "ok, for this connection string, use the value coming from here" instead of duplicating it ? At least for ADO.Net ?
还是有更好的方法?
谢谢
推荐答案
我注意到您提到了Entity Framework。可以使用与应用程序/ Web配置中定义的SQL连接不同的SQL连接创建实体上下文。 (我必须仔细阅读代码)。但是,对于更一般的具有实际上引用了另一个连接字符串的连接字符串的问题,我想说的是,如果您没有源,那么运气就不好了。
I noticed you mentioned Entity Framework. It is possible to create entity contexts with different SQL connections than the one defined in the application/web config. (I'd have to dig up the code). However, as far as the more general "have a connection string that actually references another connection string" question, I'd say, if you don't have the source, tough luck.
如果您的DLL不引用应用程序/ Web配置设置,则可能会更好,对于其中定义的类,您可以传入应用程序密钥,其中应用程序密钥具有引用a的关联值连接字符串,SQL连接字符串本身,一个SqlConnection实例或您组成的某些SqlConnection-Factory-Thing。
It'd probably be better if your DLLs didn't reference app/web config settings and, for the classes defined in there, you could pass in either the, the "application key" which has an associated value referencing a connection string, the SQL connection string itself, a SqlConnection instance, or some SqlConnection-Factory-Thing you make up.
这篇关于是否可以在另一个连接字符串中引用一个连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!