【问题标题】:Disposing data table?处理数据表?
【发布时间】:2014-08-08 18:04:09
【问题描述】:

我今天正在阅读this msdn article,我很好奇终于放弃了处理数据表。这真的有必要吗?我们为什么要处理数据表?如果我退出函数或子程序,它不会释放资源吗?

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

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

【问题讨论】:

    标签: vb.net ado.net dataset


    【解决方案1】:

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

    将事物设置为 null 或 none 不会删除对象实例使用的内存。也不处置。 GC 在运行时执行。请记住,当您有一个引用变量时,您就有一个指向对象的指针。当你将该变量重新指向一个空地址时,它不会删除前一个地址的数据,也不会释放内存。

    基本上,dispose 对于准备要销毁的对象很有用,但它实际上并没有释放对象使用的内存。如果您正在处理需要清理的资源,例如打开的流、数据库连接等,那么 dispose 非常棒。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-19
      • 1970-01-01
      • 2016-03-08
      • 2017-03-22
      • 2023-03-24
      • 2016-02-13
      • 2015-10-03
      相关资源
      最近更新 更多