0

我想做一个存储过程,然后得到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”,但未提供该参数。

怎么了 ?

4

1 回答 1

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;
 .....
于 2012-12-05T14:12:27.187 回答