【发布时间】: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
【问题讨论】: