简体   繁体   中英

Dispose objects before losing scope(SqlCommand)

How can i dispose of "command" before returning it without affecting the return type?
"Reability error CA2000 ( CA2000: Dispose objects before losing scope )"

        SqlCommand command = new SqlCommand(sqlCmd);
        command.CommandTimeout = 240;
        if (applicationId == int.MinValue)
            command.Parameters.AddWithValue("@ApplicationId", DBNull.Value);
        else
            command.Parameters.AddWithValue("@ApplicationId", applicationId);


        return DB.ExecuteDataset(command);

in the style of Jeapordy: "What is a using statement?"

using(SqlCommand command = new SqlCommand(sqlCmd))
{
    command.CommandTimeout = 240;
    if (applicationId == int.MinValue)
        command.Parameters.AddWithValue("@ApplicationId", DBNull.Value);
    else
        command.Parameters.AddWithValue("@ApplicationId", applicationId);


    return DB.ExecuteDataset(command);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM