C# SQLConnection 池 [英] C# SQLConnection pooling

查看:23
本文介绍了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 池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆