Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 03-27-2009, 07:05 PM   #1
Baseband Member
 
Join Date: Nov 2008
Posts: 91
Default displaying an auto-incremented SQL identity column

Most databases have multiple users. For these databases it is a good idea to auto-increment the identity column.

I understand that an SQL identity column is not incremented until the newly-created record is inserted.

I guess this means the identity column is incremented when you actually reconnect to the database and insert the new record there, rather than just add the record to the locally-resident DataSet, correct?

And I guess this also means that for auto-incremented identity columns it is not possible to accurately display the newly-incremented identity column in the data entry WinForm used to create the new record and add it to the database, right?

For what itís worth, the environment Iím working in is:

32-bit
SQL Server 2008 Express with Advanced Services
SQL2008 AdventureWorks
SQL Server 2008 Management Studio Express
Visual C# 2008 Express
__________________

BobLewiston is offline   Reply With Quote
Old 03-27-2009, 07:48 PM   #2
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: displaying an auto-incremented SQL identity column

You can retreive the identity of the most recently inserted record by using the @@identity property (in SQL Server), but this can't be done until the database has actually had a record inserted into it.
__________________

__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 03-27-2009, 08:38 PM   #3
Baseband Member
 
Join Date: Nov 2008
Posts: 91
Default Re: displaying an auto-incremented SQL identity column

Daeva:

Even if I did that, it would probably take at least a minute to manually type in the data in the new record that's going to be inserted, and within that minute a number of other records may have been inserted into the database by other data entry people, so that the identity column displayed in any record that's in the process of being input could very easily be out of date by the time that record is ready to actually be inserted into the database.

So I guess what I'm saying is that it would most probably be unwise to even attempt to display an identity column value for a new record that's going to be inserted, right?
BobLewiston is offline   Reply With Quote
Old 03-27-2009, 08:54 PM   #4
In Runtime
 
Daeva's Avatar
 
Join Date: Dec 2005
Posts: 407
Send a message via AIM to Daeva Send a message via MSN to Daeva Send a message via Yahoo to Daeva
Default Re: displaying an auto-incremented SQL identity column

I would say that would be very unwise (and maybe a little bit unnecessary?). As a "workaround" you could run a select statement on the current database and sort the records in descending order by the identity column, then grab the first row returned (which would be your newest record) and then add 1 to that and display this number.

But as you suggested, this information becomes inaccurate if somebody else is also modifying the database. Is there a big reason you chose to use a dataset as opposed to a custom library to communicate with the database?
__________________
**Official Self-proclaimed glorified excessive (insert additional adjectives here) post editor/modifier.
Edit = Best feature ever
http://www.twitter.com/xDaevax
Daeva is offline   Reply With Quote
Old 03-27-2009, 09:58 PM   #5
Baseband Member
 
Join Date: Nov 2008
Posts: 91
Default Re: displaying an auto-incremented SQL identity column

Daeva:

Quote:
Is there a big reason you chose to use a dataset as opposed to a custom library to communicate with the database?
Because I'm a newbie and I'm just trying to follow an online tutorial, and that's what the tutorial teaches.

What can you tell me about applicable custom libraries and their advantages (and disadvantages) compared to datasets, and what options in general are available for accessing SQL databases? I understand that it is possible to communicate "directly" with databases (rather than initialize and use canned command strings like dataAdapter.UpdateCommand.CommandText and dataAdapter.InsertCommand.CommandText), and that this approach is said to be simpler. Can you suggest the simplest way to accessing SQL databases, with perhaps a few words about more sophisticated ways to do so, under what circumstances such more sophisticated methods might be necessary, and a little of the syntax I would need for these methods, so that I can research them after I master the simplest methods? Any help you could give would be greatly appreciated.
BobLewiston is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 02:06 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0