Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Feb 21, 2012 12:36 AM by Decker Dong - MSFT
Feb 19, 2012 10:52 AM|LINK
I am not using a stored procedure and I am having a couple of problems for some reason. I have a autogenerated primary key and I am trying to get the primary key as a return value. I have
string sqlStatement = "INSERT INTO [CUSTOMER_ORDER] (USER_ID, ORDER_TIME) VALUES (@USER_ID, @ORDER_TIME, @ORDER_ID)";
Then I try to add the orderID which is the primary key but this is where
SqlParameter paramOrderID = new SqlParameter("@ORDER_ID", SqlDbType.Int);
paramOrderID.Direction = ParameterDirection.Output;
Any help would be appreciated.
Feb 19, 2012 11:05 AM|LINK
first after ur insert query use SCOPE_IDENTITY() to get auto generated id...
CREATE PROCEDURE [dbo].[Customer_Insert]
@CustomerID INT OUTPUT
INSERT INTO dbo.Customer ([Name], Email, Phone)
SET @CustomerID = CAST(SCOPE_IDENTITY() AS INT)
u can receive this....with ur output value..add after command.ExecuteNonQuery();
dim return as string
command.ExecuteNonQuery(); remove and use result =Cmd.ExecuteScalar
or u can use this..
Feb 21, 2012 12:36 AM|LINK
In my mind，I think you have two ways：
1）Just use select @@identity from xxx and use SqlDataReader+SqlCommand to finish reading the specific value。
2）Since your Order_Id is auto-generated，so plz use
select top 1 order_id from xxx order by order_id to read out the single value。