重置数据表中的自动增量 [英] Reset AutoIncrement in DataTable

查看:25
本文介绍了重置数据表中的自动增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I populate a DataSet with a DataAdapter to a SQL CE Database. Data is displayed on a DataGrid which is binded to DataSet's DataTable. I have an auto-increment ID field (or in SQLCE, called PRIMARY KEY IDENTITY) in my DataSource; correspondingly, I also set a AutoIncrement ID column in my DataTable.

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

Problem come when I clear my DataTable. I want to reset the AutoIncrement Counter back to 1 but not able to, I tried the following:

/* 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;

It just left with the counter it left off before Clear(). How can I reset the AutoIncrement in the DataTable?

解决方案

Use like this

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

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

Check the below link for more

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

这篇关于重置数据表中的自动增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆