搜索
首页 电脑/网络 软件

TableAdapter.insert()调用需要引用什么数据集(using.

TableAdapter.insert()调用需要引用什么数据集(using...)

全部回答

2017-09-28

53 0
    在修改并验证了数据集中的数据后,可能需要将更新后的数据发回数据库。要将修改后的数据发送到数据库,需要调用 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 应用程序中的窗体上显示数据在应用程序中编辑数据验证数据保存数据 。
    。

类似问题换一批

热点推荐

热度TOP

相关推荐
加载中...

热点搜索 换一换

电脑/网络
软件
硬件
电脑装机
程序设计
互联网
操作系统/系统故障
笔记本电脑
反病毒
百度
软件
软件
多媒体软件
系统软件
办公软件
网络软件
图像处理软件
举报
举报原因(必选):
取消确定举报