• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

VB.NET DataSet.Merge方法代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了VB.NET中System.Data.DataSet.Merge方法的典型用法代码示例。如果您正苦于以下问题:VB.NET DataSet.Merge方法的具体用法?VB.NET DataSet.Merge怎么用?VB.NET DataSet.Merge使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Data.DataSet的用法示例。



在下文中一共展示了DataSet.Merge方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的VB.NET代码示例。

示例1: DemonstrateMerge

Private Sub DemonstrateMerge()
     ' Create a DataSet with one table, two columns, and three rows.
     Dim dataSet As New DataSet("dataSet")
     Dim table As New DataTable("Items")
     Dim idColumn As New DataColumn("id", Type.GetType("System.Int32"))
     idColumn.AutoIncrement = True
     Dim itemColumn As New DataColumn("Item", Type.GetType("System.Int32"))

     ' DataColumn array to set primary key.
     Dim keyColumn(0) As DataColumn
     Dim row As DataRow

     ' Create variable for temporary DataSet. 
     Dim changeDataSet As DataSet

     ' Add columns to table, and table to DataSet.   
     table.Columns.Add(idColumn)
     table.Columns.Add(itemColumn)
     dataSet.Tables.Add(table)

     ' Set primary key column.
     keyColumn(0) = idColumn
     table.PrimaryKey = keyColumn

     ' Add ten rows.
     Dim i As Integer
     For i = 0 To 9
         row = table.NewRow()
         row("Item") = i
         table.Rows.Add(row)
     Next i

     ' Accept changes.
     dataSet.AcceptChanges()
     PrintValues(dataSet, "Original values")

     ' Change two row values.
     table.Rows(0)("Item") = 50
     table.Rows(1)("Item") = 111

     ' Add one row.
     row = table.NewRow()
     row("Item") = 74
     table.Rows.Add(row)

     ' Insert code for error checking. Set one row in error.
     table.Rows(1).RowError = "over 100"
     PrintValues(dataSet, "Modified and New Values")

     ' If the table has changes or errors, create a subset DataSet.
     If dataSet.HasChanges(DataRowState.Modified Or DataRowState.Added) _
        And dataSet.HasErrors Then

         ' Use GetChanges to extract subset.
         changeDataSet = dataSet.GetChanges(DataRowState.Modified _
            Or DataRowState.Added)
         PrintValues(changeDataSet, "Subset values")

         ' Insert code to reconcile errors. In this case, reject changes.
         Dim changeTable As DataTable
         For Each changeTable In  changeDataSet.Tables
             If changeTable.HasErrors Then
                 Dim changeRow As DataRow
                 For Each changeRow In  changeTable.Rows
                     'Console.WriteLine(changeRow["Item"]);
                     If CInt(changeRow("Item", _
                        DataRowVersion.Current)) > 100 Then
                         changeRow.RejectChanges()
                         changeRow.ClearErrors()
                     End If
                 Next changeRow
             End If
         Next changeTable
         PrintValues(changeDataSet, "Reconciled subset values")

         ' Merge changes back to first DataSet.
         dataSet.Merge(changeDataSet)
         PrintValues(dataSet, "Merged Values")
     End If
 End Sub
 
Private Sub PrintValues(dataSet As DataSet, label As String)
     Console.WriteLine(ControlChars.Cr & label)
     Dim table As DataTable
     For Each table In  dataSet.Tables
         Console.WriteLine("TableName: " & table.TableName)
         Dim row As DataRow
         For Each row In  table.Rows
             Dim column As DataColumn
             For Each column In  table.Columns
                 Console.Write(ControlChars.Tab & " " _
                    & row(column).ToString())
             Next column
             Console.WriteLine()
         Next row
     Next table
End Sub
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:97,代码来源:DataSet.Merge


示例2: DemonstrateMergeTable

Private Sub DemonstrateMergeTable()
    ' Create a DataSet with one table, two columns, 
    ' and ten rows.
    Dim dataSet As New DataSet("dataSet")
    Dim table As New DataTable("Items")

    ' Add tables to the DataSet
    dataSet.Tables.Add(table)

    ' Add columns
    Dim c1 As New DataColumn("id", Type.GetType("System.Int32"), "")
    Dim c2 As New DataColumn("Item", Type.GetType("System.Int32"), "")
    table.Columns.Add(c1)
    table.Columns.Add(c2)

    ' DataColumn array to set primary key.
    Dim keyCol(0) As DataColumn

    ' Set primary key column.
    keyCol(0) = c1
    table.PrimaryKey = keyCol

    ' Add RowChanged event handler for the table.
    AddHandler table.RowChanged, AddressOf Row_Changed

    ' Add ten rows.
    Dim i As Integer
    Dim row As DataRow

    For i = 0 To 9
        row = table.NewRow()
        row("id") = i
        row("Item") = i
        table.Rows.Add(row)
    Next i

    ' Accept changes.
    dataSet.AcceptChanges()
    PrintValues(dataSet, "Original values")

    ' Create a second DataTable identical to the first.
    Dim t2 As DataTable
    t2 = table.Clone()

    ' Add three rows. Note that the id column can't be the 
    ' same as existing rows in the DataSet table.
    Dim newRow As DataRow
    newRow = t2.NewRow()
    newRow("id") = 14
    newRow("Item") = 774
    t2.Rows.Add(newRow)

    newRow = t2.NewRow()
    newRow("id") = 12
    newRow("Item") = 555
    t2.Rows.Add(newRow)

    newRow = t2.NewRow()
    newRow("id") = 13
    newRow("Item") = 665
    t2.Rows.Add(newRow)

    ' Merge the table into the DataSet.
    Console.WriteLine("Merging")
    dataSet.Merge(t2)
    PrintValues(dataSet, "Merged With Table")
 End Sub 
     
 Private Sub Row_Changed( _
    sender As Object, e As DataRowChangeEventArgs)
     Console.WriteLine("Row Changed " & e.Action.ToString() _
        & ControlChars.Tab & e.Row.ItemArray(0).ToString())
 End Sub
 
 Private Sub PrintValues(dataSet As DataSet, label As String)
     Console.WriteLine(ControlChars.Cr & label)
     Dim table As DataTable
     Dim row As DataRow
     Dim column As DataColumn
     For Each table In  dataSet.Tables
         Console.WriteLine("TableName: " & table.TableName) 
         For Each row In  table.Rows             
             For Each column In  table.Columns
                 Console.Write(ControlChars.Tab & " " _
                    & row(column).ToString())
             Next column
             Console.WriteLine()
         Next row
     Next table
 End Sub
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:90,代码来源:DataSet.Merge


示例3: DemonstrateMerge

Private Sub DemonstrateMerge()
    ' Create a DataSet with one table, two columns, 
    ' and three rows.
    Dim dataSet As New DataSet("dataSet")
    Dim table As New DataTable("Items")
    Dim idColumn As New DataColumn("id", _
        Type.GetType("System.Int32"), "")
    idColumn.AutoIncrement = True
    Dim itemColumn As New DataColumn("Item", _
        Type.GetType("System.Int32"), "")

    ' Create DataColumn array to set primary key.
    Dim keyColumn(0) As DataColumn
    Dim row As DataRow

    ' Create variable for temporary DataSet. 
    Dim changesDataSet As DataSet

    ' Add RowChanged event handler for the table.
    AddHandler table.RowChanged, AddressOf Row_Changed
    dataSet.Tables.Add(table)
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)

    ' Set primary key column.
    keyColumn(0) = idColumn
    table.PrimaryKey = keyColumn

    ' Add ten rows.
    Dim i As Integer
    For i = 0 To 9
        row = table.NewRow()
        row("Item") = i
        table.Rows.Add(row)
    Next i

    ' Accept changes.
    dataSet.AcceptChanges()
    PrintValues(dataSet, "Original values")

    ' Change row values.
    table.Rows(0)("Item") = 50
    table.Rows(1)("Item") = 111

    ' Add one row.
    row = table.NewRow()
    row("Item") = 74
    table.Rows.Add(row)

    ' Insert code for error checking. Set one row in error.
    table.Rows(1).RowError = "over 100"
    PrintValues(dataSet, "Modified and New Values")

    ' If the table has changes or errors, create a subset DataSet.
    If dataSet.HasChanges(DataRowState.Modified Or DataRowState.Added) _
        And dataSet.HasErrors Then
        ' Use GetChanges to extract subset.
        changesDataSet = dataSet.GetChanges( _
            DataRowState.Modified Or DataRowState.Added)
        PrintValues(changesDataSet, "Subset values")

        ' Insert code to reconcile errors. In this case, reject changes.
        Dim changesTable As DataTable
        For Each changesTable In  changesDataSet.Tables
            If changesTable.HasErrors Then
                Dim changesRow As DataRow
                For Each changesRow In  changesTable.Rows
                    'Console.WriteLine(changesRow["Item"]);
                    If CInt(changesRow("Item", _
                        DataRowVersion.Current)) > 100 Then
                        changesRow.RejectChanges()
                        changesRow.ClearErrors()
                    End If
                Next changesRow
            End If
        Next changesTable

        ' Add a column to the changesDataSet.
        changesDataSet.Tables("Items").Columns.Add( _
            New DataColumn("newColumn"))
        PrintValues(changesDataSet, "Reconciled subset values")

        ' Merge changes back to first DataSet.
        dataSet.Merge(changesDataSet, False, _
            System.Data.MissingSchemaAction.Add)
    End If
    PrintValues(dataSet, "Merged Values")
End Sub
        
 Private Sub Row_Changed(sender As Object, e As DataRowChangeEventArgs)
     Console.WriteLine("Row Changed " + e.Action.ToString() _
        + ControlChars.Tab + e.Row.ItemArray(0).ToString())
 End Sub
    
Private Sub PrintValues(dataSet As DataSet, label As String)
     Console.WriteLine(label + ControlChars.Cr)
     Dim table As DataTable
     For Each table In  dataSet.Tables
         Console.WriteLine("TableName: " + table.TableName)
         Dim row As DataRow
         For Each row In  table.Rows
             Dim column As DataColumn
             For Each column In  table.Columns
                 Console.Write(ControlChars.Tab & " " _
                    & row(column).ToString())
             Next column
             Console.WriteLine()
         Next row
     Next table
End Sub
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:110,代码来源:DataSet.Merge


示例4: DemonstrateMergeMissingSchema

Private Sub DemonstrateMergeMissingSchema()
    ' Create a DataSet with one table, two columns, 
    ' and three rows.
    Dim dataSet As New DataSet("dataSet")
    Dim table As New DataTable("Items")
    Dim idColumn As New DataColumn("id", _
        Type.GetType("System.Int32"))
    idColumn.AutoIncrement = True
    Dim itemColumn As New DataColumn("Item", _
        Type.GetType("System.Int32"))

    ' DataColumn array to set primary key.
    Dim keyColumn(0) As DataColumn
    Dim row As DataRow

    ' Create variable for temporary DataSet. 
    Dim changeDataSet As DataSet

    ' Add RowChanged event handler for the table.
    AddHandler table.RowChanged, AddressOf Row_Changed
    dataSet.Tables.Add(table)
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)

    ' Set primary key column.
    keyColumn(0) = idColumn
    table.PrimaryKey = keyColumn

    ' Add ten rows.
    Dim i As Integer
    For i = 0 To 9
        row = table.NewRow()
        row("Item") = i
        table.Rows.Add(row)
    Next i

    ' Accept changes.
    dataSet.AcceptChanges()
    PrintValues(dataSet, "Original values")

    ' Change row values.
    table.Rows(0)("Item") = 50
    table.Rows(1)("Item") = 111

    ' Add one row.
    row = table.NewRow()
    row("Item") = 74
    table.Rows.Add(row)

    ' Insert code for error checking. Set one row in error.
    table.Rows(1).RowError = "over 100"
    PrintValues(dataSet, "Modified and New Values")
    ' If the table has changes or errors, 
    ' create a subset DataSet.
    If dataSet.HasChanges(DataRowState.Modified Or DataRowState.Added) _
        And dataSet.HasErrors Then
        ' Use GetChanges to extract subset.
        changeDataSet = dataSet.GetChanges(DataRowState.Modified _
            Or DataRowState.Added)
        PrintValues(changeDataSet, "Subset values")
        ' Insert code to reconcile errors. In this case, reject changes.
        Dim changeTable As DataTable
        For Each changeTable In  changeDataSet.Tables
            If changeTable.HasErrors Then
                Dim changeRow As DataRow
                For Each changeRow In  changeTable.Rows
                    If CInt(changeRow("Item", _
                        DataRowVersion.Current)) > 100 Then
                        changeRow.RejectChanges()
                        changeRow.ClearErrors()
                    End If
                Next changeRow
            End If
        Next changeTable

        ' Add a column to the changeDataSet to change the schema.
        changeDataSet.Tables("Items").Columns.Add( _
            New DataColumn("newColumn"))
        PrintValues(changeDataSet, "Reconciled subset values")

        ' Add values to the rows for each column.
        Dim rowItem As DataRow
        For Each rowItem In  changeDataSet.Tables("Items").Rows
            rowItem("newColumn") = "my new schema value"
        Next rowItem

        ' Merge changes back to first DataSet.
        dataSet.Merge(changeDataSet, False, _
            System.Data.MissingSchemaAction.Add)
    End If
    PrintValues(dataSet, "Merged Values")
End Sub
    
Private Sub Row_Changed(sender As Object, _
    e As DataRowChangeEventArgs)
     Console.WriteLine("Row Changed " & e.Action.ToString() _
        & ControlChars.Tab & e.Row.ItemArray(0).ToString())
End Sub
 
Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)
        Dim row As DataRow
        For Each row In  table.Rows
            Dim column As DataColumn
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " _
                    & row(column).ToString())
            Next column
            Console.WriteLine()
        Next row
    Next table
End Sub
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:115,代码来源:DataSet.Merge


示例5: DemonstrateMergeTableAddSchema

Private Sub DemonstrateMergeTableAddSchema()
    ' Create a DataSet with one table, two columns, 
    'and ten rows.
    Dim dataSet As New DataSet("dataSet")
    Dim table As New DataTable("Items")

    ' Add tables to the DataSet
    dataSet.Tables.Add(table)

    ' Create and add two columns to the DataTable
    Dim idColumn As New DataColumn("id", _
        Type.GetType("System.Int32"), "")
    idColumn.AutoIncrement = True
    Dim itemColumn As New DataColumn("Item", _
        Type.GetType("System.Int32"), "")
    table.Columns.Add(idColumn)
    table.Columns.Add(itemColumn)

    ' DataColumn array to set primary key.
    Dim keyCol(0) As DataColumn

    ' Set primary key column.
    keyCol(0) = idColumn
    table.PrimaryKey = keyCol

    ' Add RowChanged event handler for the table.
    AddHandler table.RowChanged, AddressOf Row_Changed

    ' Add ten rows.
    Dim i As Integer
    Dim row As DataRow

    For i = 0 To 9
        row = table.NewRow()
        row("Item") = i
        table.Rows.Add(row)
    Next i

    ' Accept changes.
    dataSet.AcceptChanges()
    PrintValues(dataSet, "Original values")

    ' Create a second DataTable identical to the first
    ' with one extra column using the Clone method.
    Dim cloneTable As New DataTable
    cloneTable = table.Clone()

    ' Add column.
    cloneTable.Columns.Add("extra", _
        Type.GetType("System.String"))

    ' Add two rows. Note that the id column can't be the 
    ' same as existing rows in the DataSet table.
    Dim newRow As DataRow
    newRow = cloneTable.NewRow()
    newRow("id") = 12
    newRow("Item") = 555
    newRow("extra") = "extra Column 1"
    cloneTable.Rows.Add(newRow)
        
    newRow = cloneTable.NewRow()
    newRow("id") = 13
    newRow("Item") = 665
    newRow("extra") = "extra Column 2"
    cloneTable.Rows.Add(newRow)

    ' Merge the table into the DataSet.
    Console.WriteLine("Merging")
    dataSet.Merge(cloneTable, False, MissingSchemaAction.Add)
    PrintValues(dataSet, "Merged With Table, Schema Added")
End Sub
  
Private Sub Row_Changed(sender As Object, _
    e As DataRowChangeEventArgs)
    Console.WriteLine("Row Changed " & e.Action.ToString() _
        & ControlChars.Tab & e.Row.ItemArray(0).ToString())
End Sub
    
Private Sub PrintValues(dataSet As DataSet, label As String)
    Console.WriteLine(ControlChars.Cr & label)
    Dim table As DataTable
    Dim row As DataRow
    Dim column As DataColumn
    For Each table In  dataSet.Tables
        Console.WriteLine("TableName: " & table.TableName)
        For Each row In  table.Rows             
            For Each column In  table.Columns
                Console.Write(ControlChars.Tab & " " _
                    & row(column).ToString())
            Next column
            Console.WriteLine()
        Next row
    Next table
 End Sub
开发者ID:VB.NET开发者,项目名称:System.Data,代码行数:94,代码来源:DataSet.Merge



注:本文中的System.Data.DataSet.Merge方法示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
VB.NET DataView.Find方法代码示例发布时间:2022-05-26
下一篇:
VB.NET DataSet.Clone方法代码示例发布时间:2022-05-26
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap