下面是我的存储过程。当我测试它时,总是得到正确的结果。
ALTER PROCEDURE [dbo].[AddSmoothieIngredients]
-- Add the parameters for the stored procedure here
@Query NVARCHAR(4000) ,
@SmoothieId INT ,
@CreatedDate DATETIME ,
@Status INT ,
@UserId INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
BEGIN TRY
BEGIN TRAN
IF @SmoothieId > 0
BEGIN
DELETE FROM dbo.SmoothieIngredients
WHERE SmoothieId = @SmoothieId;
EXECUTE (@Query);
END
ELSE
BEGIN
IF @UserId = 0
SET @UserId = NULL;
INSERT INTO dbo.Smoothie
( Name, CreatedDate, Status, UserId )
VALUES ( N'', @CreatedDate, @Status, @UserId );
SET @SmoothieId = SCOPE_IDENTITY();
SET @Query = REPLACE(@Query, 'sId', @SmoothieId);
EXECUTE (@Query);
END
COMMIT TRAN
RETURN @SmoothieId
END TRY
BEGIN CATCH
ROLLBACK
END CATCH
END但是,当我使用dapper.net调用这个存储过程时,总是返回-1。
using (var conn = OpenConnection())
{
var parameter = new { Query = query, SmoothieId = smoothieId, CreatedDate = createdDate, Status = status, UserId = userId };
return conn.Execute("AddSmoothieIngredients", parameter, commandType: CommandType.StoredProcedure);
}dapper.net可能无法从存储过程中提取返回值。但是我真的不知道怎么解决这个问题。请帮帮忙。
发布于 2012-09-10 08:09:38
找到解决方案了,这是我在网上找到的示例代码。而且它是有效的。
var p = new DynamicParameters();
p.Add("@a", 11);
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output);
p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure);
int b = p.Get<int>("@b");
int c = p.Get<int>("@c"); https://stackoverflow.com/questions/12343660
复制相似问题