c# - 如何将 rowstate 属性设置为“在运行时已删除”?

标签 c# asp.net

我需要在运行时将行状态属性设置为删除,需要将行状态修改为已删除。

据我所知,row.Delete() 方法调用将行状态更改为已删除,但它也会删除该行。

这里我只想将行状态更改为已删除但不应删除数据行:

示例代码:

 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 开始:

The RowState becomes Deleted after you use the Delete method on an existing DataRow. It remains Deleted until you call AcceptChanges. At this time, the DataRow is removed from the table.

所以只需调用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]);

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

相关文章:

c# - 如何使用Bass库将音频转换为另一种格式(使用C#)

c# - viewstate 在 response.redirect 上消失

c# - Application Insights 异常和故障

c# - jquery触发c#函数

c# - 如何使用额外按钮更改多个按钮的背景颜色

c# - 将音频录制到内存流中,然后将其保存到文件中

C# 抽象类命名约定

c# - 在 C# 中解析 JSON

asp.net - 如何使用 Visual Studio 2019 发布 asp.net 应用程序?

c# - 模拟非虚拟发布模型属性和方法