Re: how to get the new identity ID just after insert a new row?

  • Thread starter Phil Winstanley [Microsoft MVP ASP.NET]
  • Start date
P

Phil Winstanley [Microsoft MVP ASP.NET]

Quentin,

You'll need to use SCOPE_IDENTITY() (SQL Server) or SELECT @@IDENTITY
(SQL Server and Access) at the end of your INSERT Statement, as so: -

INSERT INTO .............; SELECT SCOPE_IDENTITY();

Hth,
Phil Winstanley
Microsoft ASP.NET MVP
http://www.myservicescentral.com
 
Q

Quentin Huo

Thanks for quick reply!

Do I need to change any code ?



strng sql = "INSERT INTO .............; SELECT SCOPE_IDENTITY();";


OleDbConnection conn = new OleDbConnection();


conn.ConnectionString = Session["oleconn"].ToString();

conn.Open();


OleDbCommand cmd = new OleDbCommand(sql,conn);

int nNoAdded = cmd.ExecuteNonQuery();



Here nNoAdded will be the ID? Just make sure.

Q.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top