c# - 重置数据表中的自动增量

标签 c# datatable

我使用 DataAdapterDataSet 填充到 SQL CE 数据库。数据显示在绑定(bind)到 DataSet 的 DataTable 的 DataGrid 上。我的数据源中有一个自动递增 ID 字段(或在 SQLCE 中,称为 PRIMARY KEY IDENTITY);相应地,我还在我的 DataTable 中设置了一个 AutoIncrement ID 列。

/* when DataTable is first populated, start counting from Rows.Count */
/* if empty, starts with 1 */
dt.Column["id"].AutoIncrementSeed = dt.Rows.Count + 1;

当我清除数据表时出现问题。我想将 AutoIncrement Counter 重置回 1 但不能,我尝试了以下操作:

/* clearing and disposing DataTable, DataSet, DataAdaptor does not reset the counter */
dt.Clear();
dt.Dispose();
ds.Clear();
ds.Dispose()
da.Dispose()

/* manually re-setting the AutoIncrementSeed also does not reset the counter */
dt.Column["id"].AutoIncrementSeed = 1;

它刚刚离开了它在 Clear() 之前离开的计数器。如何重置 DataTable 中的 AutoIncrement?

最佳答案

这样使用

dt.Clear();
dt.Column["id"].AutoIncrementStep = -1;
dt.Column["id"].AutoIncrementSeed = -1;

dt.Column["id"].AutoIncrementStep = 1;
dt.Column["id"].AutoIncrementSeed = 1;

查看以下链接了解更多

http://www.eggheadcafe.com/community/aspnet/10/25407/autoincrementseed.aspx

关于c# - 重置数据表中的自动增量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5972699/

相关文章:

c# - 从数据库中填充函数字典

javascript - 使用 DataTable search() 搜索唯一值

javascript - 数据表根据另一个值计算列总计

.net - 如何不显示继承的 DataTable 的 Visual Studio 设计器

c# - 如何获取动态添加到 Windows 窗体 C# 中的控件的值?

c# - EF Code First From DB 不为所有表创建实体

c# - DataTable.ImportRow 不添加行

javascript - 如何禁用在特定 DataTable 列中的搜索?

c# - 为什么 Assembly.GetExecutingAssembly() 会返回 null?

c# - 如何刷新页面中的 ViewModel