【发布时间】:2011-01-17 20:55:34
【问题描述】:
第一个问题:
说我有
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();
}
连接是否关闭?因为从技术上讲,我们永远不会像之前的return 那样到达最后一个}。
第二个问题:
这次我有:
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*/ }
现在,在try 的某个地方说,我们收到一个错误并被捕获。连接是否仍然关闭?因为同样,我们跳过try 中的其余代码,直接转到catch 语句。
我对@987654328@ 的工作方式的思考是否过于线性?即当我们离开using 范围时,Dispose() 是否会被调用?
【问题讨论】:
标签: c# using sqlconnection