gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 21:26:35 25 4
gpt4 key购买 nike

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

据我所知,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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com