是否可以在另一个连接字符串中引用一个连接字符串?

发布于 2024-11-16 13:55:16 字数 1209 浏览 1 评论 0原文

我们有一个包含多个 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=&quot;Data Source=localhost;Initial Catalog=DATABASE;Persist Security Info=True;User ID=USER;Password=PASSWORD;MultipleActiveResultSets=True&quot;" 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断舍离 2024-11-23 13:55:16

我注意到你提到了实体框架。可以使用与应用程序/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文