处理数据表?

时间:2014-06-18 12:27:50

标签: vb.net ado.net dataset

我今天正在阅读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

1 个答案:

答案 0 :(得分:3)

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

将thing设置为null或者什么都不会删除对象实例使用的内存。处置也没有。 GC运行时会这样做。请记住,当您有一个引用变量时,您有一个指向对象的指针。当您将该变量重新指定为空地址时,它不会删除先前地址处的数据,也不会释放内存。

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

相关问题