当前位置: 首页>>代码示例>>C#>>正文


C# DataSet.GetChanges方法代码示例

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


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

示例1: DemonstrateMerge

private void DemonstrateMerge()
   {
       // Create a DataSet with one table, two columns, 
       // and three rows.
       DataSet dataSet = new DataSet("dataSet");
       DataTable table = new DataTable("Items");
       DataColumn idColumn = new DataColumn("id", 
           Type.GetType("System.Int32"),"");
       idColumn.AutoIncrement=true;
       DataColumn itemColumn = new DataColumn("Item", 
           Type.GetType("System.Int32"),"");

       // DataColumn array to set primary key.
       DataColumn[] keyColumn= new DataColumn[1];
       DataRow row;

       // Create variable for temporary DataSet. 
       DataSet changesDataSet;

       // Add RowChanged event handler for the table.
       table.RowChanged+=new DataRowChangeEventHandler(
           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.
       for(int i = 0; i <10;i++)
       {
           row=table.NewRow();
           row["Item"]= i;
           table.Rows.Add(row);
       }
       // 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 | 
           DataRowState.Added)&& dataSet.HasErrors)
       {
           // Use GetChanges to extract subset.
           changesDataSet = dataSet.GetChanges(
               DataRowState.Modified|DataRowState.Added);
           PrintValues(changesDataSet, "Subset values");

           // Insert code to reconcile errors. In this case, reject changes.
           foreach(DataTable changesTable in changesDataSet.Tables)
           {
               if (changesTable.HasErrors)
               {
                   foreach(DataRow changesRow in changesTable.Rows)
                   {
                       //Console.WriteLine(changesRow["Item"]);
                       if((int)changesRow["Item",DataRowVersion.Current ]> 100)
                       {
                           changesRow.RejectChanges();
                           changesRow.ClearErrors();
                       }
                   }
               }
           }
           // 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);
       }
       PrintValues(dataSet, "Merged Values");
   }

   private void Row_Changed(object sender, DataRowChangeEventArgs e)
   {
       Console.WriteLine("Row Changed " + e.Action.ToString() 
           + "\table" + e.Row.ItemArray[0]);
   }

   private void PrintValues(DataSet dataSet, string label)
   {
       Console.WriteLine(label + "\n");
       foreach(DataTable table in dataSet.Tables)
       {
           Console.WriteLine("TableName: " + table.TableName);
           foreach(DataRow row in table.Rows)
           {
               foreach(DataColumn column in table.Columns)
               {
                   Console.Write("\table " + row[column] );
               }
               Console.WriteLine();
           }
       }
   }
开发者ID:.NET开发者,项目名称:System.Data,代码行数:112,代码来源:DataSet.GetChanges

示例2: UpdateDataSet

private void UpdateDataSet(DataSet dataSet)
{
    // Check for changes with the HasChanges method first.
    if(!dataSet.HasChanges(DataRowState.Modified)) return;

    // Create temporary DataSet variable and
    // GetChanges for modified rows only.
    DataSet tempDataSet = 
        dataSet.GetChanges(DataRowState.Modified);

    // Check the DataSet for errors.
    if(tempDataSet.HasErrors)
    {
        // Insert code to resolve errors.
    }
    // After fixing errors, update the data source with  
    // the DataAdapter used to create the DataSet.
    adapter.Update(tempDataSet);
}
开发者ID:.NET开发者,项目名称:System.Data,代码行数:19,代码来源:DataSet.GetChanges


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