第一个问题:
说我有
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();
}
Run Code Online (Sandbox Code Playgroud)
连接是否关闭?因为从技术上讲,我们永远不会}像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*/ }
Run Code Online (Sandbox Code Playgroud)
现在,说出来try我们得到一个错误,它被抓住了.连接是否仍然关闭?因为我们再次跳过其余代码try并直接转到catch …