473,387 Members | 1,899 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Synchronize the identity columns with SQL server

Hi, I'll try to simplify the problem:

I created a table "TestTable" in a database on the SQL server. The first column, 'id', is the primary key with a auto-increment identity. I want to work connection-less, so I made my dataset where I populated a table with the data from the TestTable. When I insert a new row with the .NewRow() method, the identity column value is the next value available. For instance: if the last row had the value of 105, the inserted rows identity value will be 106, and so on.
But if I deleted a few last rows in the table in the database (in example rows with identity values 106 and 107), after executing a SQL insert statement the new inserted rows identity value will 108, not 106 ! I hope I as clear. For the sake of simplicity I ommited why it is important to me to have the 'real' value of the identity before updating to the database.

How can I keep these values synchronized, without having to update the datasource after every inserted row in the dataset table ?

Thanks.
Nov 16 '05 #1
3 3130
Hi,

That's pretty much impossible, IF there are 2 clients using the same DB,
both will generate the same PKs in theirs dataset, at the update at least
one will have problems with the currently used values and those in the DB.

Other thing that will affect your described escenario are the deleted rows,
if you can delete them you will create "holes" in the PK, and this is pretty
much unavoidable I think.

If you don;t want to leave "holes" in the PK numbering don;t delete the
rows, just use a bool column and mark the rows as valid/invalid

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"dusty" <du***@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
Hi, I'll try to simplify the problem:

I created a table "TestTable" in a database on the SQL server. The first column, 'id', is the primary key with a auto-increment identity. I want to
work connection-less, so I made my dataset where I populated a table with
the data from the TestTable. When I insert a new row with the .NewRow()
method, the identity column value is the next value available. For instance:
if the last row had the value of 105, the inserted rows identity value will
be 106, and so on. But if I deleted a few last rows in the table in the database (in example rows with identity values 106 and 107), after executing a SQL insert
statement the new inserted rows identity value will 108, not 106 ! I hope I
as clear. For the sake of simplicity I ommited why it is important to me to
have the 'real' value of the identity before updating to the database.
How can I keep these values synchronized, without having to update the datasource after every inserted row in the dataset table ?
Thanks.

Nov 16 '05 #2
I disagree... it is not impossible to coordinate this using connectionless
methods... but it *is* a design issue, not a code issue.

Design issue: Don't use autonumber fields in the database. Use the
UniqueIdentifier datatype.

Code implementation: Have the client code generate the GUID, and place it
into the correct column both in the primary and secondary tables.

Then, simply insert the data into the correct tables.

No need to maintain a connection.

This may not be feasable for the OP, if he or she has an existing database
that cannot be changed. However, if this is new development, this is a much
better way to coordinate adds due to lower costs of connection and more
scalable implementation.

--- Nick

"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:el**************@TK2MSFTNGP09.phx.gbl...
Hi,

That's pretty much impossible, IF there are 2 clients using the same DB, both will generate the same PKs in theirs dataset, at the update at least
one will have problems with the currently used values and those in the DB.

Other thing that will affect your described escenario are the deleted rows, if you can delete them you will create "holes" in the PK, and this is pretty much unavoidable I think.

If you don;t want to leave "holes" in the PK numbering don;t delete the
rows, just use a bool column and mark the rows as valid/invalid

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"dusty" <du***@discussions.microsoft.com> wrote in message
news:E1**********************************@microsof t.com...
Hi, I'll try to simplify the problem:

I created a table "TestTable" in a database on the SQL server. The first column, 'id', is the primary key with a auto-increment identity. I want to
work connection-less, so I made my dataset where I populated a table with
the data from the TestTable. When I insert a new row with the .NewRow()
method, the identity column value is the next value available. For

instance: if the last row had the value of 105, the inserted rows identity value will be 106, and so on.
But if I deleted a few last rows in the table in the database (in
example rows with identity values 106 and 107), after executing a SQL insert
statement the new inserted rows identity value will 108, not 106 ! I hope I as clear. For the sake of simplicity I ommited why it is important to me to have the 'real' value of the identity before updating to the database.

How can I keep these values synchronized, without having to update the

datasource after every inserted row in the dataset table ?

Thanks.


Nov 16 '05 #3
> Code implementation: Have the client code generate the GUID, and place it
into the correct column both in the primary and secondary tables.


Thats it !!!! Thanks a million ! Thats what i've been looking for !!
Allthough I've kept the autoincrement, I use the GUID only temporary, to maintain the the releation in the dataset, untill its updated.

Thanks again !!!!

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jan van Veldhuizen | last post by:
I have an application which is running fine with MS SqlServer, but it should be working with Oracle as weel. At a lot of places we rely upon the ADO Recordset to return incremented identity...
112
by: Andy | last post by:
Hi All! We are doing new development for SQL Server 2000 and also moving from SQL 7.0 to SQL Server 2000. What are cons and pros for using IDENTITY property as PK in SQL SERVER 2000? Please,...
2
by: Edward | last post by:
SQL 7.0 I have a form in ASP.NET and I want to write the values to the SQL Server tables. The tables are Customer and Address tables. There will be an insert into the Customer table, and I...
5
by: newtophp2000 | last post by:
I have been using the following query to identify the IDENTITY columns in a given table. (The query is inside an application.) select column_name from information_schema.columns where...
6
by: alex via SQLMonster.com | last post by:
Hi, anybody can help me. How can i synchronize 2 tables on 2 different sql servers 2000 i mean TABLE1(col1, col2, col3, col4) and TABLE1(col1, col2, col3, col4, col5, col6) the first 3...
17
by: Trevor Best | last post by:
I don't know if this has been reported before but it appears to be a bug with Access. If I create two tables both with an identity column then create an insert trigger on table1 that inserts a...
1
by: dusty | last post by:
Hi, I'll try to simplify the problem: I created a table "TestTable" in a database on the SQL server. The first column, 'id', is the primary key with a auto-increment identity. I want to work...
41
by: pb648174 | last post by:
In a multi-user environment, I would like to get a list of Ids generated, similar to: declare @LastId int select @LastId = Max(Id) From TableMania INSERT INTO TableMania (ColumnA, ColumnB)...
1
by: rbarber | last post by:
I have to synchronize 2 databases hourly but am having difficulty maintaining foreign key relations. These tables use auto-increment columns as primary keys, with child records in other tables...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.