是否可以在另一个连接字符串中引用一个连接字符串?
我们有一个包含多个 DLL 的项目。在每个 DLL 中,我们连接到一个数据库 - 对于客户端来说始终是相同的数据库。
结果,我们现在有 3 个几乎相同的连接字符串:一个用于我们的网站,一个用于 ado.net,一个用于 telerik 报告:
<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 实体框架连接,所以客户端可能有 10 个连接字符串?
有没有办法说“好吧,对于这个连接字符串,使用来自这里的值”而不是重复它?至少对于 ADO.Net 来说是这样?
或者有更好的方法吗?
谢谢
We have a project with multiple DLLs. In each DLL, we connect to a database - always the same one for a client.
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" />
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 ?
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 ?
Or is there a better way ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我注意到你提到了实体框架。可以使用与应用程序/Web 配置中定义的 SQL 连接不同的 SQL 连接来创建实体上下文。 (我必须挖掘代码)。但是,就更一般的“有一个实际上引用另一个连接字符串的连接字符串”问题而言,我想说,如果您没有源代码,那么运气不好。
如果您的 DLL 不引用应用程序/Web 配置设置,并且对于其中定义的类,您可以传入“应用程序密钥”,它具有引用连接字符串的关联值,这可能会更好SQL 连接字符串本身、SqlConnection 实例或您编写的某些 SqlConnection-Factory-Thing。
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.
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.