Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

i need to get the last inseted id in my table. how to do it. pls help
Posted

write query
SQL
select top 1 id from table_name order by id desc
 
Share this answer
 
v2
hi ...
i cant not done ....
C#
con.Open();
            str = "select Num from Pic";
            com = new OleDbCommand(str, con);
            OleDbDataReader reader = com.ExecuteReader();
            reader.Read();
            label1.Text = reader["Num"].ToString() + "  Num";

this show num 1 ...
how change to get last num ....
please help !
 
Share this answer
 
You have already got answer of your problem but following link would be helpful for you in future to get clear idea about how can get last inserted id.

Interview Question: What are the Differences between @@IDENTITY, SCOPE_IDENTITY() and IDENT_CURRENT()[^]
 
Share this answer
 
try the below codes:

SQL
SELECT MAX(id) FROM [tablename]

OR
SQL
SELECT TOP 1 id FROM [tablename] ORDER BY id DESC

OR
SQL
--INSERT STATEMENT HERE
SELECT @@IDENTITY


Regards,
Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900