如何使用TableAdapter 更新数据

如何使用TableAdapter 更新数据,第1张

在修改并验证了数据集中的数据后,可能需要将更新后的数据发回数据库。要将修改后的数据发送到数据库,需要调用 TableAdapter 的Update 方法。此适配器的 Update 方法将更新单个数据表并根据该表中每个数据行的 RowState 执行正确的命令(INSERT、UPDATE 或 DELETE)。 注意由于尝试使用数据集的内容更新数据源可能会导致错误,因此应将调用该适配器的 Update 方法的代码放置在 try/catch 块的内部。根据您的业务需要,更新数据源的确切过程可能会有所不同,但是您的应用程序应该包括以下步骤: 在try/catch 块中调用适配器的 Update 方法。如果捕获到异常,则找到引发错误的数据行。有关更多信息,请参见如何:定位出错的行。协调数据行中的问题(在可能的情况下以编程的方式进行,或者将无效的行显示给用户进行修改),然后重新尝试更新(HasErrors、GetErrors)。将数据保存到数据库调用TableAdapter 的 Update 方法,传递数据表的名称,该数据表包含要写入数据库的值。 通过TableAdapter 用数据集更新数据库在try/catch 块中包含适配器的 Update 方法。下面的示例演示如何尝试用 NorthwindDataSet 中的Customers 表的内容从 try/catch 块内部执行更新。C#VBtry { this.Validate()this.customersBindingSource.EndEdit()this.customersTableAdapter.Update(this.northwindDataSet.Customers)MessageBox.Show("Update successful")} catch (System.Exception ex) { MessageBox.Show("Update failed")} J# try { this.Validate()this.customersBindingSource.EndEdit()this.northwindDataSetCustomersTableAdapter.Update(this.northwindDataSet.get_Customers())MessageBox.Show("Update successful")} catch (System.Exception ex) { MessageBox.Show("Update failed")} 使用TableAdapter 更新数据集中的两个相关表当更新数据集中的相关表时,务必要以正确的顺序进行更新,以减小违反引用完整性约束的可能性。命令执行的顺序也将遵循数据集中 DataRowCollection 的索引的顺序。为了防止引发数据完整性错误,最佳的做法是按照下面的顺序更新数据库:子表:删除记录。父表:插入、更新和删除记录。子表:插入和更新记录。注意如果要更新两个或更多相关表,则应将所有更新逻辑包括在一个事务内。事务是指一个过程,它首先确保对数据库的所有相关更改均可成功完成,然后再提交更改。有关更多信息,请参见执行事务。使用TableAdapter 更新两个相关表创建三个临时数据表以保存不同的记录。从try/catch 块中为每个子行集调用 Update 方法。如果发生更新错误,则应分支出来并解决这些错误。 将更改提交到数据库。处置临时数据表以释放资源。下面的示例显示如何用包含相关表的数据集更新数据源。C#VBvoid 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()} } } J# void UpdateDB() { NorthwindDataSet.OrdersDataTable deletedChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.get_Orders().GetChanges(DataRowState.Deleted)NorthwindDataSet.OrdersDataTable newChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.get_Orders().GetChanges(DataRowState.Added)NorthwindDataSet.OrdersDataTable modifiedChildRecords = (NorthwindDataSet.OrdersDataTable)northwindDataSet.get_Orders().GetChanges(DataRowState.Modified)try { if (deletedChildRecords != null) { northwindDataSetOrdersTableAdapter.Update(deletedChildRecords)} northwindDataSetCustomersTableAdapter.Update(northwindDataSet.get_Customers())if (newChildRecords != null) { northwindDataSetOrdersTableAdapter.Update(newChildRecords)} if (modifiedChildRecords != null) { northwindDataSetOrdersTableAdapter.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()} } } 请参见概念TableAdapter 概述“显示数据”概述其他资源数据演练连接到 Visual Studio 中的数据准备应用程序以接收数据将数据获取到应用程序在Windows 应用程序中的窗体上显示数据在应用程序中编辑数据验证数据保存数据

access数据默认会复制一个副本到当前应用程序的目录,并且以后程序运行修改的是该副本数据库,你修改完后再次启动应用程序,原来未修改的数据库又会被复制到当前目录覆盖修改的副本数据库,所以你看不出程序数据更改。

在解决方案资源管理器中,选择你的数据库,在“复制到输出目录”属性中选择“不复制”或“如果较新则复制”即可


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9992917.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存