解决方法
虽然你可能还不知道,但你正确地做到了.
这可能听起来像很多开销,但sql Server的.NET Framework数据提供程序中的连接池实际上会为您优化这一点.
实际上建议关闭连接.
以下是文档中的引用:
It is recommended that you always
close the Connection when you are
finished using it in order for the
connection to be returned to the pool.
This can be done using either the
Close or Dispose methods of the
Connection object. Connections that
are not explicitly closed might not be
added or returned to the pool. For
example,a connection that has gone
out of scope but that has not been
explicitly closed will only be
returned to the connection pool if the
maximum pool size has been reached and
the connection is still valid.
以下是一些执行此操作的代码示例:
try { conn.Open(); // Perform query here } finally { conn.Close(); }
以供参考:
http://msdn.microsoft.com/en-us/library/8xx3tyca(VS.71).aspx