哪个是向数据集中的表添加新行的最佳方法? [英] Which is the best way to add a new row to a table in the dataset?

查看:82
本文介绍了哪个是向数据集中的表添加新行的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是出于好奇而想知道哪一个是向MyDataSet中的表添加新行的更好方法:



 私有  Sub  AddNewRow()
Dim NewDataRowView as DataRowView = MyBindingSource.AddNew()
Dim NewDataRow As MyDataset.MyTableRow = NewDataRowView.Row
NewDataRow.BeginEdit()
使用 NewDataRow
.fkCreatedBy = intLoggedOnUserID
.dtCreatedDate = DateTime.Now
结束 使用
NewDataRow.EndEdit()
MyBindingSource.EndEdit()
结束 Sub









 私有  Sub  AddNewRow()
Dim MyDataTable as MyDataset.MyTable
Dim NewDataTableRow As MyTableRow = MyDataTable.NewDataTableRow
使用 NewDataTableRow
.fkCreatedBy = intLoggedOnUserID
。 dtCreatedDate = DateTime.Now
结束 使用
MyDataTable.AddNewDataTableRow(NewDataTableRow)
结束 Sub





我尝试过的事情:



我试过了两次,我不确定是否有任何真正的差异,但我来自更有经验的人,这是最佳实践。

解决方案

您应该向DataTable添加行。



DataViews通常用于从DataTable获取子集(或所有)记录,如果编辑了其中任何一个,则更改将保存到基础数据表&安培;然后是数据源。

通常,DataView用于过滤和/或排序DataTable中的数据

虽然可以向DataView添加新行,但您需要考虑为什么你有额外的外推水平和因为他们会更新基础数据表的复杂性 - 参考; DataViews | Microsoft Docs [ ^ ]



希望这有帮助


Just wondered out of curiosity which one of these is the better way to Add a New row to a table in MyDataSet:

Private Sub AddNewRow()
    Dim NewDataRowView as DataRowView = MyBindingSource.AddNew()
    Dim NewDataRow As MyDataset.MyTableRow = NewDataRowView.Row
    NewDataRow.BeginEdit()
    With NewDataRow
        .fkCreatedBy = intLoggedOnUserID
        .dtCreatedDate = DateTime.Now
    End With
    NewDataRow.EndEdit()
    MyBindingSource.EndEdit()
End Sub



OR

Private Sub AddNewRow()
    Dim MyDataTable as MyDataset.MyTable
    Dim NewDataTableRow As MyTableRow = MyDataTable.NewDataTableRow
    With NewDataTableRow
        .fkCreatedBy = intLoggedOnUserID
        .dtCreatedDate = DateTime.Now
    End With
    MyDataTable.AddNewDataTableRow(NewDataTableRow)
End Sub



What I have tried:

I have tried both and i am not sure if there is any real difference but i am here to find out from more experienced people which is best practice.

解决方案

You should add rows to a DataTable.

DataViews are normally used to get a subset (or all) records from a DataTable, if any of these are edited then the changes would be saved to the underlying data table & then the data source.
Typically DataViews are used for filtering and/or sorting data in a DataTable
While you can add new rows to a DataView you would need to consider why you have the extra level of extrapolation & complexity as they would update the underlying datatable anyway - refer; DataViews | Microsoft Docs[^]

Hope this helps


这篇关于哪个是向数据集中的表添加新行的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆