C#的SQLConnection池 [英] C# SQLConnection pooling
问题描述
谁能介绍我怎么做的ADO.Net连接池,我也需要连接到3个独立的数据库。其中2个是在相同的服务器和其他在一个单独的1
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.
更好地与code snipts ..
Better with code snipts..
推荐答案
只要的你是严密的关于处理您的连接,默认(对于SQL服务器至少)是它只是自动工作。在您的例子中,你很可能只需要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.
要的禁用的池(如果您选择),包括池=假;
在连接字符串
To disable pooling (if you choose), include Pooling=false;
in the connection-string.
这篇关于C#的SQLConnection池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!