获取不同版本的DataRow

这段代码展示了如何在C#中使用DataRow类获取数据集(DataSet)中不同版本的数据行,包括删除状态的行。通过HasVersion方法检查数据行是否存在特定版本,如Default和Deleted,并从中获取数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//获取不同版本的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);
  }
 }
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值