简体   繁体   English

处置我需要从我的方法返回的数据表对象

[英]Dispose datatable object which i need to return from my method

DataTable dt= new Datatable();
try {
    SqlCommand Cmd = new SqlCommand("sp_getData",SqlCon);
    SqlCommand.CommandType= CommandType.StroedProcedure;
    SqlCon.Open();
    sqlDataReader dr=  cmd.ExecuteReader();
    dt.Load(dr);
    SqlCon.Close();
}
catch(Exception ex) {
}
finally{
    dt.Dispose() //
}

return dt;

Is this code legitimate ?... My method is returning that datatable object , so after calling dispose will that retain the value ??.. please explain.. 这段代码合法吗?...我的方法正在返回该数据表对象,因此在调用dispose之后将保留值?? ..请解释。

No. You will be returning an already-disposed object. 不会。您将返回已经放置的对象。

If you are returning an IDisposable it is perfectly normal to expect it to be the caller's responsibility to dispose of the object when they are through with it. 如果您要返回一个IDisposable那么通常情况下,调用者有责任在物品通过时将其丢弃。 Therefore, you should return the DataTable object as is, undisposed, and let the calling code dispose of it. 因此,您应该按原样返回DataTable对象,并且让调用代码处理它。

Normally this would be done with a using block. 通常这将使用使用块来完成。 For example: 例如:

class MyDisposableObject : IDisposable { /*...*/ }

public MyDisposableObject MakeMeAnObject() { return new MyDisposableObject(); }

public void Main()
{
    using(var o = MakeMeAnObject())
    {
        o.Foo = 1;
        o.Bar = 2;
        o.FooBar();
    }
}

Note I do see some local IDisposable objects in your snippet that you are not disposing but should be. 注意我确实在您的代码段中看到了一些您没有处理但应该处理的本地IDisposable对象。 You are also swallowing exceptions. 您还吞下了异常。

this will give you what you want: 这会给你你想要的东西:

    public DataTable getDataTable()
    {
        using (SqlConnection sqlCon = new SqlConnection("connectionString"))
        using (SqlCommand cmd = new SqlCommand("sp_getData", sqlCon))
        {
            try
            {
                cmd.CommandType = CommandType.StoredProcedure;
                sqlCon.Open();
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    return dt;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            return null;
        }
    }

This will return a disposed object which isn't what you want. 这将返回一个不符合您要求的处置对象。

One thing you can do is pass a delegate, which will allow you to work on the DataTablr without returning it, and still dispose within your original data method. 您可以做的一件事就是传递一个委托,该委托将使您无需返回数据就可以处理DataTablr,并且仍然可以将其放置在原始数据方法中。

Pseudo-code: 伪代码:

public void GetSomeData(Action<DataTable> processDataTableAction)
{
    try
    { 
        ... ( get the data )
        processDataTableAction(dt);
    }
    catch
    {
        // handle exceptions
    }
    finally
    {
        dt.Dispose();
    }
}

Then to call the function elsewhere in code: 然后在代码中的其他地方调用该函数:

GetSomeData(dt => {
    // do stuff with dt
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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