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 池


- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 使用 rss + c# 2022-01-01