Click here to Skip to main content
15,917,709 members
Home / Discussions / Database
   

Database

 
QuestionObtaining value of identity field when inserting into SQL Pin
Dewald28-Nov-07 22:49
Dewald28-Nov-07 22:49 
Hi all,

I've been trying to find a similar question but couldn't find one so forgive me if this has already been dealt with.

Is there a way that I can immediately obtain the value of an auto incrementing identity field in SQL if I insert a new record?

Let's say I run the following command:
SqlCommand myCommand = new SqlCommand("INSERT INTO Users (UserName, Age) VALUES ('Joe Bloggs', 30)", myConnection);
but the Users table has an auto incrementing identity field called UserID. Is there a way I can obtain the new value of UserID without having to run a subsequent SELECT UserID FROM Users WHERE... query?

Thanks
AnswerRe: Obtaining value of identity field when inserting into SQL Pin
Colin Angus Mackay28-Nov-07 23:07
Colin Angus Mackay28-Nov-07 23:07 
GeneralRe: Obtaining value of identity field when inserting into SQL Pin
Tobias Schoenig28-Nov-07 23:31
Tobias Schoenig28-Nov-07 23:31 
GeneralRe: Obtaining value of identity field when inserting into SQL Pin
Colin Angus Mackay28-Nov-07 23:40
Colin Angus Mackay28-Nov-07 23:40 
GeneralRe: Obtaining value of identity field when inserting into SQL Pin
Dewald29-Nov-07 0:16
Dewald29-Nov-07 0:16 
QuestionRank(sql) Pin
r a j u u28-Nov-07 22:44
r a j u u28-Nov-07 22:44 
AnswerRe: Rank(sql) Pin
pmarfleet29-Nov-07 0:07
pmarfleet29-Nov-07 0:07 
Question[Message Deleted] Pin
gubba28-Nov-07 22:42
gubba28-Nov-07 22:42 
AnswerRe: Regarding retreiving values from table Pin
pmarfleet29-Nov-07 0:01
pmarfleet29-Nov-07 0:01 
Questioninsert new row in sql server Pin
break_day28-Nov-07 20:20
break_day28-Nov-07 20:20 
AnswerRe: insert new row in sql server Pin
yaad fridman28-Nov-07 20:51
yaad fridman28-Nov-07 20:51 
GeneralRe: insert new row in sql server Pin
break_day28-Nov-07 21:13
break_day28-Nov-07 21:13 
GeneralRe: insert new row in sql server Pin
yaad fridman28-Nov-07 21:40
yaad fridman28-Nov-07 21:40 
GeneralRe: insert new row in sql server Pin
break_day28-Nov-07 23:01
break_day28-Nov-07 23:01 
GeneralRe: insert new row in sql server Pin
yaad fridman1-Dec-07 19:58
yaad fridman1-Dec-07 19:58 
AnswerRe: insert new row in sql server Pin
ChandraRam28-Nov-07 21:19
ChandraRam28-Nov-07 21:19 
QuestionExport Table to Xml Using Sql Pin
Nagaraju_Focus28-Nov-07 20:09
Nagaraju_Focus28-Nov-07 20:09 
AnswerRe: Export Table to Xml Using Sql Pin
Maharishi Bhatia28-Nov-07 20:56
Maharishi Bhatia28-Nov-07 20:56 
GeneralRe: Export Table to Xml Using Sql Pin
Nagaraju_Focus28-Nov-07 22:23
Nagaraju_Focus28-Nov-07 22:23 
GeneralRe: Export Table to Xml Using Sql Pin
Tobias Schoenig29-Nov-07 1:58
Tobias Schoenig29-Nov-07 1:58 
AnswerRe: Export Table to Xml Using Sql Pin
Maharishi Bhatia1-Dec-07 16:35
Maharishi Bhatia1-Dec-07 16:35 
Questionremote database accessing Pin
dayya28-Nov-07 18:18
dayya28-Nov-07 18:18 
AnswerRe: remote database accessing Pin
pmarfleet28-Nov-07 19:45
pmarfleet28-Nov-07 19:45 
AnswerRe: remote database accessing Pin
Paul Conrad29-Nov-07 13:00
professionalPaul Conrad29-Nov-07 13:00 
QuestionGet size of a table row Pin
Soumya Mathew28-Nov-07 18:10
Soumya Mathew28-Nov-07 18:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.