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 池


基础教程推荐
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01