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

Forum statistics

Threads
474,268
Messages
2,571,096
Members
48,773
Latest member
Kaybee

Latest Threads

Top