C# SQL连接池

发布于 2024-10-07 20:40:48 字数 96 浏览 0 评论 0原文

谁能告诉我如何在 ADO.Net 中进行连接池,我确实需要连接到 3 个独立的数据库。其中 2 个位于同一服务器中,另一个位于单独的服务器中。

最好有代码片段..

Can anyone brief me how to do Connection Pooling in ADO.Net, I do need to connect to 3 separate databases. 2 of them are in same server and the other in a separate one.

Better with code snipts..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

猫卆 2024-10-14 20:40:48

只要您严格处理连接,默认情况(至少对于 sql-server 来说)是它会自动工作。在您的示例中,您很可能只有 3 个底层连接(每个连接字符串一个)。

始终确保您的连接被释放,最好使用using

using(var conn = new SqlConnection(connectionString)) {
    // use conn
}

然后将其释放回池(以便在下次看到相同的连接字符串时重新使用)当抛出异常时。

禁用池化(如果您选择),请在连接字符串中包含Pooling=false;

as long as you are strict about disposing your connections, the default (for sql-server at least) is that it will just work automatically. In your example you could well only have 3 underlying connections (one per connection string).

But always ensure your connections are disposed, ideally with using:

using(var conn = new SqlConnection(connectionString)) {
    // use conn
}

then it is released back to the pool (for re-use when the same connection-string is seen next) even when an exception is thrown.

To disable pooling (if you choose), include Pooling=false; in the connection-string.

永不分离 2024-10-14 20:40:48

不需要特别配置或设置任何东西,就让它发生...我见过的大多数问题都是由于人们没有关闭连接或太聪明引起的

池是每个连接字符串创建的,所以你会在这个中有三个案件

Don't need to configure or set anything especially, just let it happen... Most issues I've seen are caused by folk not closing connections or being too clever

The pools are created per connection string so you'd have three in this case

千笙结 2024-10-14 20:40:48

某些提供商提供连接池。据我所知,这包括 Microsoft Access 数据库 (Jet) 和 SQL Server CE(精简版)的连接字符串。

由于缺乏连接池,数据库访问速度明显变慢。

对于缺少 Jet 池的问题,我的一种解决方案是在整个程序生命周期中始终打开与 Jet 数据库的一个连接(与推荐的技术相反)。这极大地加快了来自 .NET 应用程序的 Jet Access 数据库 SQL 查询速度,但也仅适用于本地 Jet 数据库;对于网络共享上的 Jet 数据库,我会相对较快地收到有关打开文件句柄过多的异常。

对于SQL Server CE,我到目前为止还没有找到解决方案。

Certain providers do not provide connection pooling. To my knowledge this includes connection strings to Microsoft Access Databases (Jet) and SQL Server CE (Compact edition).

The lack of connection pooling for those make database access remarkably slower.

A solution to the missing Jet pooling was for me to always have one single connection to the Jet database open for the whole program lifetime (contrary to what the recommended technique is). This speeded up my Jet Access database SQL queries from .NET applications tremendously, but also only works on local Jet databases; for Jet databases on network shares, I get relatively fast exceptions about too many open file handles.

For SQL Server CE, I found no solution so far.

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