Groups | Blog | Home
all groups > dotnet ado.net > january 2007 >

dotnet ado.net : AutoIncrement Columns


Neil
1/27/2007 1:36:00 AM
Hi,

I'm wondering if it's possible to regenerate the increment id's in an
AutoIncrement Column?

My situation is this...I have a client application that is recieving a
several DataTables from a service. The DataTables contain an auto increment
column which is guaranteed to be unique within that table but I need to merge
the tables together to create a new table and still have a unique column.

Orignally I was taking the first table as a base and then adding new rows to
this for the other tables using LoadDataRow since the structure will always
be the same.

Since the format of each table is the same I'm using
LoadDataRow(myRowToAdd). I have tried creating a new AutoIncrementColumn and
LoadingDataRow but this doesn't work, I end up with NULLS in the new Column
and of course non unique values in the existing.

So I guess my question is what is the best way to create this new
autoincrement column? I'm hoping I don't have to loop through the datatables
that come in because I will be recieving updates to all this data and think
this could be quite slow.

Any help appreciated.

Neil
1/28/2007 4:46:01 AM
Hi Cor,

Thanks for the suggestion. I have used the GUID before but in this case it
won't help me. I should have mentioned why I was wanting the AutoIncrement
Column...

I have an initial dataload stage which see's my client recieve several data
tables, each of these is combined into one table and then bound to a grid. I
also will recieve updates to the initial data loads and new datarows as time
goes on. I was using the AutoIncrement column as a Row Indexer something
along the lines of code below (excuse syntax errors). This just saves me
looping through the initial data table for each update I have and hence it's
a bit quicker. Otherwise I might be looping through my initial data table
(2000 records) just to update 2 rows. Perhaps there is a better way to do
this...?

Thanks


string uniqueIdent = myUpdateRow["UniqueKey"].ToString();

DataRow foundRow = myInitialData.Find(uniqueIdent);

if(foundRow != null) //it's an update
{
int rowIdx = (int)foundRow["myAutoIncrementCol"].ToString();

//Loop through all columns in the row to see if any values have changed
using RowIdx
myInitialData.Rows[rowIdx][colIdx] != updateRow[colIdx]
}
else //New Record
{
myInitialData.LoadDataRow(updateRow);
}



[quoted text, click to view]
Cor Ligthert [MVP]
1/28/2007 11:29:25 AM
Neil,

Therefore try to forget in this century the simple autoincrement id and use
the Global Unique Identifier (GUID) in SQL the Unique Identifier to make
ID's. You know, you are not the first with the problems you are describing.

Just my thought,

Cor

"Neil" <Neil@discussions.microsoft.com> schreef in bericht
news:B5DB0625-3D92-426F-9A0F-8B310F322697@microsoft.com...
[quoted text, click to view]

Cor Ligthert [MVP]
1/28/2007 2:09:51 PM
Neil,

Are you sure that you cannot use the merge.

http://www.vb-tips.com/dbpages.aspx?ID=edb1409d-5394-468f-a63f-de3a5d92b14a

Cor


"Neil" <Neil@discussions.microsoft.com> schreef in bericht
news:B382BAF3-55AD-489D-8B2B-87148F26DF8E@microsoft.com...
[quoted text, click to view]

AMDRIT
1/29/2007 9:42:46 AM
If the merge function does not work, you can overrload it to do what you
want it to do. Set your step and increment to -1, this way you will not run
into duplicate numbers.


[quoted text, click to view]

AddThis Social Bookmark Button