我在操作数据表时,使用了row.delete方法。
可是在我还想访问这条数据时,编译器提示,不可以访问被标志为删除的数据请问如何访问。
我通过讲dataset数据写入XML发现 被删除的数据是存在的,只是换了个数据

解决方案 »

  1.   

    设置这条记录的RowState试试看
      

  2.   

    用下面语句访问你要的字段的值
    row[columnName, DataRowVersion.Original]
    实际上
    row[columnName]默认使用的是row[columnName, DataRowVersion.Current]
    参照msdn DataRowVersion的说明会很清楚。
    不过微软如果能开放部分这种顶层类的代码对于我们理解c#会更好
      

  3.   

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

    }