且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更新多个表

更新时间:2023-11-09 22:16:10

一个小小的Google大有帮助. 读我 [ ^ ]
A little Google goes a long way. Read me[^]


实际上,仅凭一个命令,您的想法/想要的确实是一种无风险的方法.

为了帮助您更多地了解数据集以及如何更有效地使用它们,我提出了这种可能的解决方案.

我会在项目中创建一个DataSet对象,例如``CustomDataSet''.这会将数据集类定义为"CustomDataSet"类型.

然后,使用现有的SELECT命令将TableAdapters添加到设计器中.您可以将问题中的所有表添加到设计器中.

允许设计人员自动生成更新,删除,插入命令(请注意sql必须用于一个表,即无联接,否则自动生成insert,delete,update无效).

然后在代码中:

There really isn''t a risk-free way of doing what your thinking/wanting, with just one command.

To help you learn a little more about Datasets and how they can be used more efficiently, I present this possible solution.

I would create a DataSet object in the project e.g. ''CustomDataSet''. This will define the data set class as type ''CustomDataSet''.

Then add the TableAdapters to the designer using your existing SELECT commands. You can add all the tables as in your question to the designer.

Allow the designer to auto generate the update, delete, insert commands (NOTE that the sql must be for one table i.e no joins, otherwise auto generation of insert,delete,update will not work).

Then in code:

Dim ds as New CustomDataSet
Dim spTa as New CustomDataSetTableAdapters.SpTableAdapter 'or whatever you call the table
Dim itemTa as New CustomDataSetTableAdapters.ItemTableAdapter

spTa.Fill(ds.sp)
itemTa.Fill(ds.Item)
'repeat for other tables

'Note the ds.sp tables rows will have RowState of 'Unchanged'. You do not need to use this but it helps to know what it is. The .Fill method sets rowstate = 'unchanged'. New rows added to the DS will be 'Added'. Updated rows will be 'Modified'. Have a play around or read up 'DataRow.RowState'

' Add row
Dim spRow as CustomDataSet.spRow = ds.sp.NewSpRow()
spRow.spId = 0
'Set other spRow fields this way. (Fields will appear in intellisense because it is defined in typed dataset CustomDataSet.

ds.sp.AddSpRow(spRow) ' <-- Here the spRow.RowState will be 'Added'

'
'Repeat the table row adds for the other tables
'

' Now call built in update method
spTa.Update(ds.Sp)
itemTa.Update(ds.Item)



请注意,一旦提交,调用.Update方法会将行状态重置为Unchanged.如果需要在执行数据库提交/更新之前操纵已修改或已添加的行,则可以使用RowState属性,然后使用DataSet.AcceptChanges()进行最终确定.



Note that calling the .Update methods will reset the rowstates to Unchanged once committed. If you need to manipulate modified or added rows before doing database commit/update, then you can use the RowState property then finalise using DataSet.AcceptChanges().