2012-12-05 159 views
0

獲得最後一個ID我想打一個存儲過程,然後讓我剛剛創建的ID(身份)錯誤從存儲過程

這是我做過什麼:

ALTER PROCEDURE [dbo].[SP_Insert_Hai_Pi] 
@id INT OUT, 
@email VARCHAR(350) = NULL, 
@datetimeNow datetime, 
@score INT = 0 
AS 

INSERT INTO TBL_HAI_PI (USER_EMAIL,DATETIME_GAME,CORRECT_ANSWERS) VALUES (@email, CURRENT_TIMESTAMP,@score) 

SET @id = SCOPE_IDENTITY(); 
return @id 

然後:

SqlCommand command = new SqlCommand("SP_Insert_Hai_Pi", conn); 
command.CommandType = CommandType.StoredProcedure; 
command.Parameters.Add("@email", SqlDbType.VarChar).Value = email; 

conn.Open(); 
int tempId = command.ExecuteNonQuery(); 
conn.Close(); 
return tempId; 

Tt的告訴我:

過程或函數'SP_Insert_Hai_Pi'需要未提供的參數'@id'。

怎麼了?

+0

你有沒有嘗試添加一個整數參數id:'INT ID; command.Parameters.AddWithValue(「@ id」,id);'? – rcdmk

回答

0

即使是輸出,你必須定義它:

..... 
command.CommandType = CommandType.StoredProcedure; 
SqlParameter outputIdParam = new SqlParameter("@ID", SqlDbType.Int) 
    { 
     Direction = ParameterDirection.Output 
    }; 

    command.Parameters.Add(outputIdParam); 
    command.Parameters.Add("@email", SqlDbType.VarChar).Value = email; 
.....