C# SQLConnection pooling(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 池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# SQLConnection 池


基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 如何激活MC67中的红灯 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01