码迷,mamicode.com
首页 > 移动开发 > 详细

Dapper: How to get return value ( output value) by call stored procedure

时间:2019-09-21 14:43:25      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:Nid   text   prim   use   tab   rip   mic   data   stat   

使用Dapper 执行存储过程插入一条数据,同时返回主键
Dapper 的参数类型有以下四种 System.Data.ParameterDirection

    public enum ParameterDirection
    {

        Input = 1,

        Output = 2,

        InputOutput = 3,

        ReturnValue = 6
    }

Method 1 Use ParameterDirection.ReturnValue

key:

return @@IDENTITY
p.Add("@ID", dbType: DbType.Int32, direction:ParameterDirection.ReturnValue);
var id = p.Get("@tID");

MyTabel:
CREATE TABLE [dbo].[WorkLog](
    [LogID] [bigint] IDENTITY(1,1) NOT NULL,
    [TypeID] [int] NOT NULL,
    [InsertDate] [datetime2](7) NOT NULL,
    [Description] [nvarchar](max) NULL,
    [UserName] [nvarchar](250) NULL,
    [StatusId] [int] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Store Procedure:
CREATE proc [dbo].[InsertLogAndReturnID]
   @TypeID INT ,
   @Description nvarchar(max),
   @UserName nvarchar(250)
   AS
  Begin
    declare @TestID INT
    
     INSERT INTO [dbo].[WorkLog]
           ( [TypeID]
           ,[InsertDate]
           ,[Description]
           ,[UserName])
     VALUES
           (
           @TypeID
           , GETDATE()
           , @Description
           ,@UserName )
    
    return @@IDENTITY   
  END
GO
C# code:
var spName = "[dbo].[InsertLogAndReturnID]";

 using (SqlConnection objConnection = new SqlConnection(Util.ConnectionString))
 {
    objConnection.Open();
    DynamicParameters p = new DynamicParameters();

    p.Add("@ID", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
    p.Add("@TypeID", 1);
    p.Add("@Description", "TEST1");
    p.Add("@UserName", "stone");

    var row = SqlMapper.Execute(objConnection, spName, p, commandType: CommandType.StoredProcedure);
     var id  = p.Get<Int32>("@ID");

      objConnection.Close();
   }

Method 1 Use ParameterDirection.Output

Stored Procedure
CREATE proc [dbo].[InsertLogAndReturnID]
   @TypeID INT ,
   @Description nvarchar(max),
   @UserName nvarchar(250),
   @ID INT OUTPUT
   AS
  Begin
    declare @TestID INT
    
     INSERT INTO [dbo].[WorkLog]
           ( [TypeID]
           ,[InsertDate]
           ,[Description]
           ,[UserName])
     VALUES
           (
           @TypeID
           , GETDATE()
           , @Description
           ,@UserName )
    
    SELECT @ID = @@IDENTITY 
  END
GO
C# Code
var spName = "[dbo].[InsertLogAndReturnID]";

 using (SqlConnection objConnection = new SqlConnection(Util.ConnectionString))
 {
    objConnection.Open();
    DynamicParameters p = new DynamicParameters();

    p.Add("@TestID", dbType: DbType.Int32, direction: ParameterDirection.Output);
    p.Add("@TypeID", 1);
    p.Add("@Description", "TEST1");
    p.Add("@UserName", "stone");

    var row = SqlMapper.Execute(objConnection, spName, p, commandType: CommandType.StoredProcedure);
     var id  = p.Get<Int32>("@TestID");

      objConnection.Close();
   }

Dapper: How to get return value ( output value) by call stored procedure

标签:Nid   text   prim   use   tab   rip   mic   data   stat   

原文地址:https://www.cnblogs.com/leestone/p/11562970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!