asp.net - Read last Inserted row in Sql according to its Time stamp -


sql has table called emp.

emp(emp_id int identity primary key, employeename varchar(50),.......) 

i want insert record above table. here code in asp.net.

dbconnection dbcon = new dbconnection(); string query = "insert emp values('" + textbox_empname.text + "','" + ....); int no1 = dbcon.insertquery(query); 

i have table called emp-relation

 emp-relation(emp_id int primary key, count int, ....)       -- foreign key (emp_id)references emp(emp_id) 

my problem when inserting emp row ,i dont know emp_id since created auto. , when going insert emp-relation , want emp-id since foreign key.

how can this? there way read last insert row in sql according time stamp or thing? believe records not sorted according inserted timestamp in nature. please me.

there's bascally 2 ways. first way return new id first insert query:

insert emp values(...) select scope_identity() newid 

the second way lookup first row when insert relation table:

insert  emp-relation         (emp_idm, ...)  select  emp_id ,       ...    emp   emp_name = @empname 

you have pass in enough columns make reference unique.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -