数据表不会更新我的数据库

Datatable won't update my DB

usersTableAdapter usersTA = new usersTableAdapter();
var usersTable = new users.usersDataTable();
usersTA.Fill(usersTable);

VK_BDayParser.users.usersRow row = usersTable.FindByid(currentItem.id);
row.BeginEdit();
row.last_name = "********";
row.EndEdit();

row.AcceptChanges();
usersTable.AcceptChanges();

int result = usersTA.Update(usersTable);

SQL 服务器 2012.

我使用从 VS 2013 生成的 类 到数据库

我尝试更新我的数据库中的行,在 'usersRow row' 中此更改有效,但在数据库中没有任何更改。我究竟做错了什么? result 始终为 0。

这是对 AcceptChanges 方法作用的常见误解。
我认为问题出在 AcceptChanges 方法文档中的初始注释。

Commits all the changes made to this table since the last time AcceptChanges was called.

很多人认为这意味着 'commits to the database table',而不是 'commits to the in-memory instance of the datatable object'。

然后,文档的以下评论暗示了那里真正发生的事情。

When AcceptChanges is called, any DataRow object still in edit mode successfully ends its edits. The DataRowState also changes: all Added and Modified rows become Unchanged, and Deleted rows are removed.

因此,DataRow.RowState 属性 表示属于 DataTable 内存中实例的内存中行的当前状态。正是这种状态帮助 DataAdapter.Update 方法发现如何处理这些行。

换句话说,Update 方法决定仅针对 RowState==DataRowState.Unchanged 之外的行更新数据库 table。但是调用 AcceptChanges 'commits' 这些行并且它们的状态变为 Unchanged。那就没更新了。