English 中文(简体)
简称表 NET和记忆问题[复制]
原标题:Closing SqlConnection in a ASP.NET and memory issue [duplicate]

In the ASP.NET app I m working with the data layer that has functions such as:

public DataSet GetFacilitiesInfo(string userName = null)
{
    DataSet Ds = new DataSet();
    string storedProcName = "FacilitySelect";

    SqlConnection sqlConn = new SqlConnection();
    SqlCommand sqlCommand;
            
    try
    {
        DBConnection cn = new DBConnection();
        sqlConn = cn.DBConnTIP(_connectionString);
        sqlCommand = new SqlCommand(storedProcName, sqlConn);
        sqlCommand.CommandType = CommandType.StoredProcedure;               

        SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCommand);
        sqlDA.Fill(Ds);

        return Ds;
    }
    catch (Exception e)
    {
        throw e;
    }
    finally
    {
        sqlConn.Close();
        sqlConn.Dispose();
    }
} 

Persoft

The Dispose method is primarily implemented to release unmanaged resources.

我在上述法典中知道,我们可以使用<代码>使用{......>},但在幕后,CIL将其翻译到完全相同的功能电话中。

我的理解是,<条码>SqlConnection(ADO.NET)是一个有管理的物体,因此,我们如何处置?

我认为我在这里没有东西。

最佳回答

我的理解是,SqlConnection(ADO.NET)是一个有管理的物体,因此我们如何处置?

The "managed" word here only refers to memory. So the memory used for the Sqlconnection object is managed and will be released appropriately, but the network, file, special shared memory, or named pipe resources used for completing the connection are not managed, and need to be disposed properly.

事实上, 任何实施<代码>的可支配的条令:

  1. Be wrapped in a using block
  2. Be wrapped in try/finally blocks
  3. Be used as a member of a type that itself implements IDisposable.

Always

www.un.org/spanish/ga/president 只有在涉及未经管理的资源的情况下才能使用可支配的代码。


As for the finally block, in this case, yes, the finally block is enough, and using is not strictly required.

然而,<代码> 副渔获物/代码> 这里的封锁绝对毫无用处......更糟糕的是,它摧毁任何最终例外的号召。 换言之,仅仅删除<条码>副渔获物/代码>栏目,不仅安全,而且,如果你这样做,你可重新填上

一旦这样做,你就可以节省更多的代码,同时完全更换<条码>、<>条码>和<条码>,定有的区块,代之以<条码/条码>的新<条码>,从此可以节省另外两条线路和一层tation,代之以新的<条码>使用瓦尔

最后,<代码>sqlConn 以前使用<新SqlConnection的物体在首次申报时分配的<代码>,该物体被彻底涂.,这种变数是前所未有的。 因此,我们也能够清理。

要求它像这样:

public DataSet GetFacilitiesInfo(string userName = null)
{
    DataSet Ds = new DataSet();
    string storedProcName = "FacilitySelect";
    DBConnection cn = new DBConnection();

    using var sqlConn = cn.DBConnTIP(_connectionString);
    using var sqlCommand = new SqlCommand(storedProcName, sqlConn);
    using var sqlDa = new SqlDataAdapter(sqlCommand);
        
    sqlDA.Fill(Ds);            
    return Ds;
}

This is safer (all the IDisposables are accounted for), has better error handling (call stack is preserved), uses noticeably less code (always a win), and with absolutely no loss of function.

就完整性而言,这里的版本也使用一些电离层,以利用<代码>用户Name<>> 代码。 论点:

public DataSet GetFacilitiesInfo(string userName = null)
{
    DataSet Ds = new DataSet();
    string storedProcName = "FacilitySelect";
    DBConnection cn = new DBConnection();

    using var sqlConn = cn.DBConnTIP(_connectionString);
    using var sqlCommand = new SqlCommand(storedProcName, sqlConn);
    using var sqlDa = new SqlDataAdapter(sqlCommand);
    
    sqlCommand.CommandType = CommandType.StoredProcedure;  
    // I have to guess the type and length
    sqlCommand.Parameters.Add("@UserName", SqlDbType.NVarchar, 25).Value = userName;
    if (userName == null) sqlCommand.Parameters["@UserName"].Value = DBNull.Value;
        
    sqlDA.Fill(Ds);            
    return Ds;
}

That said, it is somewhat common to want actual meaningful error handling in this type of method, with a real catch block. And once you do that, you re back to where a finally block is just as good.

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签