问题
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?
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).
Thanks
Farooq
Update:
ok i tried this:
MyDataSet sharedDS = new MyDataSet();
. . .
CleanDS()
{
MyDataSet referenceDS = new MyDataSet();
sharedDS.Table1.Reset();
sharedDS.Merge(referenceDS);
}
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?
回答1:
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;
please see reasoning in this thread: http://www.eggheadcafe.com/community/aspnet/10/25407/autoincrementseed.aspx
and: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.autoincrementseed(VS.85).aspx
thanks all for your help!
回答2:
Does the DataSet.Clear() method do what you want?
Edit
Because you say you want to reset the primary key, you must be talking about on the DataTables the DataSet holds. Have you tried calling DataSet.Tables["MyTable"].Reset(), which should reset the table to it's original state, rather than the whole DataSet?
Edit 2
If this is an autoincrementing column, have you tried resetting the DataColumn's AutoIncrementSeed property to 0?
来源:https://stackoverflow.com/questions/764004/reset-primary-key