在“使用”块中,SqlConnection是否在返回或异常时关闭?
第一个问题:
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
using
Dispose()
using
慕盖茨4494581
相关分类