Best way to get ID of inserted row??

G

Guest

Hi, I do a sqlcommand.executenonquery on the click of a button, which
performs an insert statement. What is the most reliable way (i.e. someone
else could insert at similar time) to get the id i have JUST created?
 
L

Lau Lei Cheong

Set a counter at another table.

When you need new number, use a while loop to select the counter then
increment the counter by 1 where it's value equal to the origional value. If
returned affected number of rows = 0, you know the number has been used by
the others then you should get the next one......
 
D

Deepak

Louise,

Use ExecuteScalar wchich returns the value of first column in the first row,
and use the scope identity function to get the identity value you have just
inserted. Below is some code to give you an idea.

create table #temp
( myid int identity,
n varchar(10)
)

insert into #temp
values('a')

select scope_identity() as [identity_value]



--
With Regards,


Deepak
[I code, therefore I am]
 
G

Guest

Thanks.

int intNewID = Convert.ToInt32(command.ExecuteScalar());

I put select_scopeidentity() onto the end of my insert statement, ran the
above code and obtained my new ID. it runs in the same connection so the ID
shouldnt belong to someone else's query, should it?

Deepak said:
Louise,

Use ExecuteScalar wchich returns the value of first column in the first row,
and use the scope identity function to get the identity value you have just
inserted. Below is some code to give you an idea.

create table #temp
( myid int identity,
n varchar(10)
)

insert into #temp
values('a')

select scope_identity() as [identity_value]



--
With Regards,


Deepak
[I code, therefore I am]

louise raisbeck said:
Hi, I do a sqlcommand.executenonquery on the click of a button, which
performs an insert statement. What is the most reliable way (i.e. someone
else could insert at similar time) to get the id i have JUST created?
 
D

Deepak

Thats right, this ID value has now been used and will not be available to
any other insert.

--
With Regards,


Deepak
[I code, therefore I am]

louise raisbeck said:
Thanks.

int intNewID = Convert.ToInt32(command.ExecuteScalar());

I put select_scopeidentity() onto the end of my insert statement, ran the
above code and obtained my new ID. it runs in the same connection so the
ID
shouldnt belong to someone else's query, should it?

Deepak said:
Louise,

Use ExecuteScalar wchich returns the value of first column in the first
row,
and use the scope identity function to get the identity value you have
just
inserted. Below is some code to give you an idea.

create table #temp
( myid int identity,
n varchar(10)
)

insert into #temp
values('a')

select scope_identity() as [identity_value]



--
With Regards,


Deepak
[I code, therefore I am]

louise raisbeck said:
Hi, I do a sqlcommand.executenonquery on the click of a button, which
performs an insert statement. What is the most reliable way (i.e.
someone
else could insert at similar time) to get the id i have JUST created?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top