处理数据表? [英] Disposing data table?

查看:94
本文介绍了处理数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这篇msdn文章今天,我很好奇最后是否同意处理数据表。那真的有必要吗?为什么要布置数据表?如果退出函数或子程序,是否不释放资源?

I was reading this msdn article today and I am curious about finally part with disposing datatable. Is that really necessary? why should we dispose a datatable? If I exit the function or sub, is it not releasing the resources?

文章中的解释是;
处理临时数据表以释放资源。

Private Sub UpdateDB()
Dim deletedChildRecords As NorthwindDataSet.OrdersDataTable = _
    CType(NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Deleted), NorthwindDataSet.OrdersDataTable)

Dim newChildRecords As NorthwindDataSet.OrdersDataTable = _
    CType(NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Added), NorthwindDataSet.OrdersDataTable)

Dim modifiedChildRecords As NorthwindDataSet.OrdersDataTable = _
    CType(NorthwindDataSet.Orders.GetChanges(Data.DataRowState.Modified), NorthwindDataSet.OrdersDataTable)

Try
    If deletedChildRecords IsNot Nothing Then
        OrdersTableAdapter.Update(deletedChildRecords)
    End If

    CustomersTableAdapter.Update(NorthwindDataSet.Customers)

    If newChildRecords IsNot Nothing Then
        OrdersTableAdapter.Update(newChildRecords)
    End If

    If modifiedChildRecords IsNot Nothing Then
        OrdersTableAdapter.Update(modifiedChildRecords)
    End If

    NorthwindDataSet.AcceptChanges()

Catch ex As Exception
    MessageBox.Show("An error occurred during the update process")
    ' Add code to handle error here.

Finally
    If deletedChildRecords IsNot Nothing Then
        deletedChildRecords.Dispose()
    End If

    If newChildRecords IsNot Nothing Then
        newChildRecords.Dispose()
    End If

    If modifiedChildRecords IsNot Nothing Then
        modifiedChildRecords.Dispose()
    End If

End Try
End Sub


推荐答案

要回答您的问题,是有必要布置数据表吗?共识是不,这是我的想法。

To answer your question, is it necessary to dispose a datatable? The concensus is no, here's my thought.

将内容设置为null或不执行任何操作都不会删除对象实例使用的内存。都不处置。 GC在运行时会这样做。请记住,当您有一个引用变量时,便有了一个指向对象的指针。当您将该变量指向空地址时,它不会删除先前地址的数据,也不会释放内存。

Setting things to null or nothing doesn't remove the memory used by the instance of the object. Neither does dispose. The GC does, when it runs. Remember, when you have a reference variable, you have a pointer to an object. When you repoint that variable to a null address, it doesn't delete the data at the previous address, nor does it free the memory.

基本上,dispose对于准备要销毁的对象,但实际上并不会释放该对象使用的内存。如果您要处理的资源需要清理,例如开放流,数据库连接之类的东西,那么处理就好了。

Basically, dispose is useful for preparing your objects for being destroyed, but it doesn't actually release the memory used by the object. If you're dealing with resources that need cleaning up, like open streams, database connections, things like that, dispose is great.

这篇关于处理数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆