发布日期: 2016年7月
在修改并验证了数据集中的数据后,可能需要将更新后的数据发回数据库。 要将修改后的数据发送到数据库,需要调用 TableAdapter 的 Update
方法。 此适配器的 Update
方法将更新单个数据表并根据该表中每个数据行的 RowState 执行正确的命令(INSERT、UPDATE 或 DELETE)。 当您将数据保存在相关表中时,Visual Studio 提供的 TableAdapterManager 组件有助于根据在数据库中定义的外键约束以正确的顺序执行保存。 有关更多信息,请参见分层更新概述。
备注
由于尝试使用数据集的内容更新数据源可能会导致错误,因此应将调用该适配器的 Update
方法的代码放入 try
/catch
块内。
根据不同的业务需求,更新数据源的确切过程可能会有所不同,但是您的应用程序应该包括以下步骤:
在
try
/catch
块中调用适配器的Update
方法。如果捕获到异常,则找到引发错误的数据行。 有关更多信息,请参见如何:定位出错的行。
解决数据行中的问题(在可能的情况下以编程方式进行,或者将无效的行提供给用户进行修改),然后重新尝试更新(HasErrors、GetErrors)。
将数据保存到数据库
调用 TableAdapter 的 Update
方法,传递数据表的名称,该数据表包含要写入数据库的值。
使用 TableAdapter 更新具有数据集的数据库
在
try
/catch
块中包含适配器的Update
方法。 下面的示例演示如何尝试用NorthwindDataSet
中的Customers
表的内容从try
/catch
块内部执行更新。try { this.Validate(); this.customersBindingSource.EndEdit(); this.customersTableAdapter.Update(this.northwindDataSet.Customers); MessageBox.Show("Update successful"); } catch (System.Exception ex) { MessageBox.Show("Update failed"); }
Try Me.Validate() Me.CustomersBindingSource.EndEdit() Me.CustomersTableAdapter.Update(Me.NorthwindDataSet.Customers) MsgBox("Update successful") Catch ex As Exception MsgBox("Update failed") End Try
使用 TableAdapter 更新数据集中的两个相关表
更新数据集中的相关表时,为了减小违反引用完整性约束的可能性,必须以正确的顺序进行更新。 命令执行的顺序也将遵循数据集中 DataRowCollection 的索引的顺序。 为了防止引发数据完整性错误,最佳做法是按照下面的顺序更新数据库:
子表:删除记录。
父表:插入、更新和删除记录。
子表:插入和更新记录。
备注
如果要更新两个或更多相关表,应将所有更新逻辑包括在一个事务内。 事务是指一个过程,它在提交任何更改之前都先要确保对数据库的所有相关更改均可成功完成。 有关更多信息,请参见 事务和并发。
使用 TableAdapter 更新两个相关表
创建三个临时数据表以保存不同的记录。
从
try
/catch
块中为每个子行集调用Update
方法。 如果发生更新错误,则应分支出来并解决这些错误。将更改提交到数据库。
处置临时数据表以释放资源。
下面的示例显示如何用包含相关表的数据集更新数据源。
void UpdateDB() { NorthwindDataSet.OrdersDataTable deletedChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Deleted); NorthwindDataSet.OrdersDataTable newChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Added); NorthwindDataSet.OrdersDataTable modifiedChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.Orders.GetChanges(DataRowState.Modified); try { if (deletedChildRecords != null) { ordersTableAdapter.Update(deletedChildRecords); } customersTableAdapter.Update(northwindDataSet.Customers); if (newChildRecords != null) { ordersTableAdapter.Update(newChildRecords); } if (modifiedChildRecords != null) { ordersTableAdapter.Update(modifiedChildRecords); } northwindDataSet.AcceptChanges(); } catch (Exception ex) { MessageBox.Show("An error occurred during the update process"); // Add code to handle error here. } finally { if (deletedChildRecords != null) { deletedChildRecords.Dispose(); } if (newChildRecords != null) { newChildRecords.Dispose(); } if (modifiedChildRecords != null) { modifiedChildRecords.Dispose(); } } }
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
请参阅
TableAdapter 概述
数据演练
在 Visual Studio 中将 Windows 窗体控件绑定到数据
连接到 Visual Studio 中的数据
准备应用程序以接收数据
将数据获取到应用程序
在 Visual Studio 中将控件绑定到数据
在应用程序中编辑数据
验证数据
保存数据