如果执行了SqlCommand并超时,则是否关闭和/或处置了相应的SqlConnection?
最佳答案
除非您将SqlConnection包装在using
语句中,否则您仍然负责关闭和处置连接(就像其他任何异常一样)。
您还可以使用try/catch/finally
块:
try
{
// Create and execute your SqlCommand here
}
catch(SqlException ex)
{
// Catch the timeout
}
finally
{
// Close and Dispose the SqlConnection you're using
}
但是
using
更加整洁并自动配置:using(SqlConnection conn = new SqlConnection())
{
// Do your work here.
// The SqlConnection will be closed and disposed at the end of the block.
}
关于sql - 如果执行了SqlCommand并超时,则是否关闭和/或处置了相应的SqlConnection?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1840358/