获取不同版本的DataRow

来源:互联网 发布:淘宝店铺联盟入口 编辑:程序博客网 时间:2024/06/11 08:43

//获取不同版本的DataRow
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Collections;
using System.Data;
using System.Xml;
using System.Management;
using System.Net;


namespace Zhzuo
{
 class ZZConsole
 {
  [STAThread]
  static void Main(string[] args)
  { 
   DataSet ds = new DataSet();
   CreatDataSetSchema(ds);
   InitData(ds);
   DataRow drdel = ds.Tables["Hosts"].Rows[0];
   drdel.Delete();
   Console.WriteLine(drdel.HasVersion(DataRowVersion.Default).ToString());
   foreach(DataRow dr in ds.Tables["Hosts"].GetChanges(DataRowState.Deleted).Rows)
   {
    //if(dr.HasVersion(DataRowVersion.Current))
    //{
     Console.WriteLine((string)dr["HId"]);
     Console.WriteLine(dr["IsLocal",DataRowVersion.Original].ToString());
    //}
   }
   Console.WriteLine("end");
   Console.ReadLine();
  }
  //初始化数据集结构
  private static void CreatDataSetSchema(DataSet ds)
  {
   DataTable dt = new DataTable("Hosts");
   DataColumn dc = new DataColumn("HId",typeof(String));
   dt.Columns.Add(dc);
   dc = new DataColumn("IsLocal",typeof(Boolean));
   dt.Columns.Add(dc);
   ds.Tables.Add(dt);
  }
  //加入数据
  private static void InitData(DataSet ds)
  {
   DataRow hostsRow = ds.Tables["Hosts"].NewRow();
   hostsRow["HId"] = "192.192.132.229";
   hostsRow["IsLocal"] = true;
   ds.Tables["Hosts"].Rows.Add(hostsRow);

   hostsRow = ds.Tables["Hosts"].NewRow();
   hostsRow["HId"] = "192.192.132.231";
   hostsRow["IsLocal"] = false;
   ds.Tables["Hosts"].Rows.Add(hostsRow);

   hostsRow = ds.Tables["Hosts"].NewRow();
   hostsRow["HId"] = "192.192.132.233";
   hostsRow["IsLocal"] = false;
   ds.Tables["Hosts"].Rows.Add(hostsRow);
  }
 }
 
}

原创粉丝点击