如何将rowstate属性设置为Deleted at Runtime

本文关键字:Deleted at Runtime 设置 属性 rowstate | 更新日期: 2023-09-27 18:28:12

我需要在运行时将rowstate属性设置为deleted。需要将rowstate Modified修改为deleted。

我知道那一排。Delete()方法调用将行状态更改为Deleted,但也会删除row。

在这里,我只想将行状态更改为Deleted,但数据行不应该被删除:

样本代码:

 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);

在这篇文章中,我得到了一个异常,即"无法通过行访问已删除的行信息。"

如何将rowstate属性设置为Deleted at Runtime

来自MSDN:

在现有DataRow。在调用AcceptChanges之前,它将保持"已删除"状态。在这一次,DataRow从表中删除。

所以只需调用Delete,直到AcceptChanges,行状态将为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]);

不,你肯定做不到。根据文档RowState被定义为只读属性(只有getter),类似

public DataRowState RowState { get; }