在"采用"块是一个SqlConnection在返回或异常关闭? [英] in a "using" block is a SqlConnection closed on return or exception?
问题描述
第一个问题:结果
说我有
使用(SqlConnection的连接=新的SqlConnection(的connectionString))
{
connection.Open(); 字符串storedProc =的GetData;
的SqlCommand命令=新的SqlCommand(storedProc,连接);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(新的SqlParameter(@雇员,雇员)); 回报(字节[])command.ExecuteScalar();
}
是否连接关闭后得到?因为从技术上,我们从来没有到最后}
因为我们收益
之前。
第二个问题:结果
这一次,我有:
尝试
{
使用(SqlConnection的连接=新的SqlConnection(的connectionString))
{
INT雇员= findEmployeeID(); connection.Open();
的SqlCommand命令=新的SqlCommand(UpdateEmployeeTable连接);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(新的SqlParameter(@雇员,雇员));
command.CommandTimeout = 5; command.ExecuteNonQuery();
}
}
赶上(例外){/ *处理错误* /}
现在,说某处尝试
我们得到一个错误,它被逮住。那么连接仍然可以关闭?因为,我们再次跳过code在剩下的尝试
键,直接进入抓
语句。
我在想太线如何使用
作品? IE浏览器的Dispose()
当我们离开使用
范围简单地称之为?
- 是
- 是
无论哪种方式,当使用块退出(通过成功完成或错误)它是封闭的。
虽然我认为这将是的的更好的以组织这样的,因为它是一个更容易,看看有什么事情发生,即使是新的维护程序员谁支持后来:
使用(SqlConnection的连接=新的SqlConnection(的connectionString))
{
INT雇员= findEmployeeID();
尝试
{ connection.Open();
的SqlCommand命令=新的SqlCommand(UpdateEmployeeTable连接);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(新的SqlParameter(@雇员,雇员));
command.CommandTimeout = 5; command.ExecuteNonQuery();
}
赶上(例外)
{
/ *处理错误* /
}}
First question:
Say I have
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
string storedProc = "GetData";
SqlCommand command = new SqlCommand(storedProc, connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID));
return (byte[])command.ExecuteScalar();
}
Does the connection get closed? Because technically we never get to the last }
as we return
before it.
Second question:
This time I have:
try
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
int employeeID = findEmployeeID();
connection.Open();
SqlCommand command = new SqlCommand("UpdateEmployeeTable", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID));
command.CommandTimeout = 5;
command.ExecuteNonQuery();
}
}
catch (Exception) { /*Handle error*/ }
Now, say somewhere in the try
we get an error and it gets caught. Does the connection still get closed? Because again, we skip the rest of the code in the try
and go directly to the catch
statement.
Am I thinking too linearly in how using
works? ie Does Dispose()
simply get called when we leave the using
scope?
- Yes
- Yes.
Either way, when the using block is exited (either by successful completion or by error) it is closed.
Although I think it would be better to organize like this because it's a lot easier to see what is going to happen, even for the new maintenance programmer who will support it later:
using (SqlConnection connection = new SqlConnection(connectionString))
{
int employeeID = findEmployeeID();
try
{
connection.Open();
SqlCommand command = new SqlCommand("UpdateEmployeeTable", connection);
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@EmployeeID", employeeID));
command.CommandTimeout = 5;
command.ExecuteNonQuery();
}
catch (Exception)
{
/*Handle error*/
}
}
这篇关于在"采用"块是一个SqlConnection在返回或异常关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!