當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET DataSet.GetChanges方法代碼示例

本文整理匯總了VB.NET中System.Data.DataSet.GetChanges方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET DataSet.GetChanges方法的具體用法?VB.NET DataSet.GetChanges怎麽用?VB.NET DataSet.GetChanges使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Data.DataSet的用法示例。


在下文中一共展示了DataSet.GetChanges方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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"), "")

    ' 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.GetChanges

示例2: UpdateDataSet

Private Sub UpdateDataSet(ByVal dataSet As DataSet)
   ' Check for changes with the HasChanges method first.
   If Not dataSet.HasChanges(DataRowState.Modified) Then 
       Exit Sub
   End If

   ' Create temporary DataSet variable and
   ' GetChanges for modified rows only.
   Dim tempDataSet As DataSet = _
       dataSet.GetChanges(DataRowState.Modified)

   ' Check the DataSet for errors.
   If tempDataSet.HasErrors Then
      ' Insert code to resolve errors.
   End If

   ' After fixing errors, update the data source with   
   ' the DataAdapter used to create the DataSet.
   adapter.Update(tempDataSet)
End Sub
開發者ID:VB.NET開發者,項目名稱:System.Data,代碼行數:20,代碼來源:DataSet.GetChanges


注:本文中的System.Data.DataSet.GetChanges方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。