C# SQLConnection 池

C# SQLConnection pooling(C# SQLConnection 池)
本文介绍了C# SQLConnection 池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何在 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.

使用代码片段更好..

推荐答案

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

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.

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

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

这篇关于C# SQLConnection 池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)