How do I return the IDENTITY for an inserted record from a stored Proecedure?

Posted by user54197 on Stack Overflow See other posts from Stack Overflow or by user54197
Published on 2010-04-07T19:27:18Z Indexed on 2010/04/07 19:33 UTC
Read the original article Hit count: 185

Filed under:
|
|

I am adding data to my database, but would like to retrieve the UnitID that is Auto generated.

using (SqlConnection connect = new SqlConnection(connections)) 
{ 
SqlCommand command = new SqlCommand("ContactInfo_Add", connect); 
command.Parameters.Add(new SqlParameter("name", name)); 
command.Parameters.Add(new SqlParameter("address", address)); 
command.Parameters.Add(new SqlParameter("Product", name)); 
command.Parameters.Add(new SqlParameter("Quantity", address)); 
command.Parameters.Add(new SqlParameter("DueDate", city)); 
connect.Open(); 
command.ExecuteNonQuery(); 
} 

...

ALTER PROCEDURE [dbo].[Contact_Add] 
@name varchar(40), 
@address varchar(60), 
@Product varchar(40), 
@Quantity varchar(5), 
@DueDate datetime 
AS  
BEGIN 
 SET NOCOUNT ON; 

 INSERT INTO DBO.PERSON 
 (Name, Address) VALUES (@name, @address) 
 INSERT INTO DBO.PRODUCT_DATA 
 (PersonID, Product, Quantity, DueDate) VALUES (@Product, @Quantity, @DueDate) 
END 

© Stack Overflow or respective owner

Related posts about sql

Related posts about stored-procedures