如何在运行时将rowstate属性设置为Deleted?

时间:2014-10-29 13:26:22

标签: c# asp.net

我需要在运行时将rowstate属性设置为删除,需要修改rowstate Modified to Deleted。

我知道row.Delete()方法调用将行状态更改为Deleted但它也删除了Row。

这里我只想将rowstate更改为Deleted但不应删除datarow:

示例代码:

 Delete delete = new Delete()
                  .from("tb_lib_report_package_link")
                  .where()
                          .add().criteria("lib_report_id  = :pLibReportId ")
                          .and().criteria("package_Id  IN (" + packageLink + ")")
                  .endWhere();
            drow.AcceptChanges();
            drow.Delete();

            DataRow[] dra = new DataRow[] { drow };

             DbAccessManager accessManager = DbAccessFactory.GetDatabaseAccess(DatabaseType.Oracle, "Data Source=SRV",
                                                                              "DEV", "dev", this.AppCookieData["UserId"]);
            accessManager.ClientId = this.AppCookieData["UserId"];
            DbConnection cn = accessManager.CreateConnection();
            delete.ParameterSetter = new ParameterSetter((builder, param, index) =>
            {
                switch (builder.GetColumnName(param))
                {
                    case "pLibReportId":
                        param.DbType = this.ServerCfgReader.DefaultDBProvider.DBDataAccess.GetDbType(drow["LIB_REPORT_ID"].GetType());
                        param.Value = drow["LIB_REPORT_ID"];
                        break;
                    default:
                        throw new NotImplementedException();
                }
            });
            accessManager.Delete(delete, dra, cn);

在此我得到的例外是“无法通过该行访问已删除的行信息。”

2 个答案:

答案 0 :(得分:3)

MSDN开始:

  

在您使用Delete方法后,RowState变为已删除   现有的DataRow。在您调用AcceptChanges之前,它仍会被删除。在   这次,DataRow将从表中删除。

所以只需致电Delete,直到AcceptChanges rowstate为Deleted,但行本身不会从数据表中删除。注意:如果要删除数据行的状态为Added - 在调用Delete

后,它将立即从数据表中删除

<强>更新

您可以像这样访问已删除的行数据:

var dt = new DataTable();
dt.Columns.Add("ID", typeof (long));
dt.Columns.Add("Name", typeof (string));

dt.Rows.Add(1, "xxx");
dt.Rows.Add(2, "yyy");

dt.AcceptChanges(); // now both rows are in 'Unchanged' state

dt.Rows[1].Delete(); // second row now in 'Deleted' state

foreach (DataRow dro in dt.Rows)
{
    if (dro.RowState == DataRowState.Deleted)
        // accessing deleted row
        Console.WriteLine("Deleted row: ID={0}, Name={1}", dro["ID", DataRowVersion.Original], dro["Name", DataRowVersion.Original]);
    else
        // accessing row as usual
       Console.WriteLine("Row: ID={0}, Name={1}", dro["ID"], dro["Name"]);
}

//putting into another array changes nothing in the manner of acessing deleted row
DataRow[] dra = new DataRow[] {dt.Rows[1]};
Console.WriteLine("Deleted row from another array: ID={0}", dra[0]["ID", DataRowVersion.Original]);

答案 1 :(得分:1)

不,你当然不能那样做。每Documentation RowState被定义为只读属性(只有getter),如

public DataRowState RowState { get; }