SqlConnection
是否得到妥善处理?
/// <summary>
/// Executes the stored procedure using the parameters.
/// </summary>
/// <param name="storedProcedureName"></param>
/// <param name="parameters"></param>
/// <param name="timeoutValue"></param>
/// <returns></returns>
public DataSet ExecuteStoredProcedure(string storedProcedureName, List<SqlParameter> parameters, int timeoutValue = 0)
{
var ds = new DataSet();
var sqlConnection = (SqlConnection)Database.Connection;
using (var command = new SqlCommand(storedProcedureName, sqlConnection))
{
//setting CommandTimeout specified value
if (timeoutValue > 0)
{
command.CommandTimeout = timeoutValue;
}
command.CommandType = CommandType.StoredProcedure;
foreach (var param in parameters)
{
command.Parameters.Add(param);
}
_databaseHelper.OpenConnection(command);
var adapter = new SqlDataAdapter(command);
adapter.Fill(ds);
_databaseHelper.CloseConnection(command);
}
return ds;
}
答案 0 :(得分:2)
嗯,你不要在这里打开一个新的连接,所以它不会被处理掉,这不是交易。
在任何情况下, SqlCommand
都会正确关闭,因为您正在使用using
构造。但是,如果此行(例如var adapter = new SqlDataAdapter(command);
)将抛出异常,_databaseHelper.CloseConnection(command);
显然不会被调用
答案 1 :(得分:1)
sqlConnection 是否得到妥善处置?
没有。 _databaseHelper.CloseConnection(command)
表示它可能包含command.Connection.Close()
调用,这相当于调用Dispose()
,但这不会被描述为“正确”,因为它不在finally
块中