gpt4 book ai didi

c# - 在具有相同值的 DataRow 上调用 SetField 会导致更改吗?

转载 作者:行者123 更新时间:2023-11-30 20:54:52 25 4
gpt4 key购买 nike

这里有些困惑,下面的代码会导致 DataTable 发生变化,而我不希望它这样做。 DataTable内部没有实际的个体值变化管理吗?还是我遗漏了什么?

var table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Rows.Add("Sample Name");
table.AcceptChanges();

table.Rows[0].SetField("Name", "Sample Name");

var changes = table.GetChanges();

// expecting null, actually returns changes

我本来期望在内部表使用一些预构建的逻辑来确定新值是否确实不同于当前值。

为什么会这样?

最佳答案

当您修改一行的内容时,它会更新 DataRow.RowState属性,它可以包含 UnChangedAddedModified 等。稍后 DataTable.GetChanges 会根据该属性过滤掉行行状态。 与值无关

DataTable.GetChanges - MSDN

Gets a copy of the DataTable containing all changes made to it since it was last loaded, or since AcceptChanges was called, filtered by DataRowState.

另见:

Row States and Row Versions - MSDN

ADO.NET manages rows in tables using row states and versions. A row state indicates the status of a row; row versions maintain the values stored in a row as it is modified, including current, original, and default values. For example, after you have made a modification to a column in a row, the row will have a row state of Modified, and two row versions: Current, which contains the current row values, and Original, which contains the row values before the column was modified.

关于c# - 在具有相同值的 DataRow 上调用 SetField 会导致更改吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18748408/

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