我需要将rowstate属性设置为在运行时删除,需要将rowstate修改为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);


在这种情况下,我遇到了一个例外,即“无法通过行访问已删除的行信息”。

最佳答案

MSDN开始:


  在行上使用Delete方法后,RowState变为Deleted
  现有的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]);

关于c# - 如何在运行时将rowstate属性设置为Deleted?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26631602/

10-16 10:17