重置主键 [英] Reset primary key

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

问题描述

我已经尝试在线上找到一个答案,但显然无法做到这一点(我的意思是在应用级别,而不是数据库)。我需要完全清除我的数据集,同时重置主键。任何想法?

I've tried to find an answer to do this online but apparently it can't be done (I mean at the app level, not database). I have a need to clear out my dataset completely and reset the primary key at the same time. Any ideas?

或者,我可以使用的一个hack是重新初始化数据集,但似乎不可能,因为数据集在应用程序中的不同类之间共享(我在Program.cs中创建共享数据集)

Alternatively, one hack i can use is to reinitialize the dataset but that doesn't seem possible as well since the dataset is shared between different classes in the app (I'm creating the shared dataset in Program.cs).

谢谢

Farooq

更新:

好的我尝试过:

MyDataSet sharedDS = new MyDataSet();

MyDataSet sharedDS = new MyDataSet();



. . .

CleanDS()

{

    MyDataSet referenceDS = new MyDataSet(); 
    sharedDS.Table1.Reset(); 
    sharedDS.Merge(referenceDS); 

}

我原来的问题解决了但现在我得到一个System.ArgumentException对于Column1不属于Table1,我可以看到数据集查看器中的列以及查看填充行。另请注意,我可以手动重新创建整个DataSet,我仍然得到相同的错误。任何想法?

My original problem is solved but now I get an System.ArgumentException for Column1 does not belong to Table1 where I can see the columns in the DataSet Viewer as well as see the populated rows. Also note that I can manually re-create the entire DataSet and I still get the same error. Any ideas?

推荐答案

我尝试使用autoincrementseed和autoincrementstep,它终于奏效了。这里是其他人的参考:

i tried it with the autoincrementseed and autoincrementstep and it finally works. here's for the reference of others:

sharedDS.Clear();
sharedDS.Table1.Columns[0].AutoIncrementStep = -1;
sharedDS.Table1.Columns[0].AutoIncrementSeed = -1;
sharedDS.Table1.Columns[0].AutoIncrementStep = 1;
sharedDS.Table1.Columns[0].AutoIncrementSeed = 1;

请参阅此线程中的推理:
http://www.eggheadcafe.com/community/aspnet/10/25407/autoincrementseed.aspx

please see reasoning in this thread: http://www.eggheadcafe.com/community/aspnet/10/25407/autoincrementseed.aspx

和:
http://msdn.microsoft.com/en-us/library/system.data.datacolumn.autoincrementseed(VS.85).aspx

感谢大家的帮助!

这篇关于重置主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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