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

使用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();
   }

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

时间: 2024-10-09 21:20:50

Dapper: How to get return value ( output value) by call stored procedure的相关文章

【2016-11-2】【坚持学习】【Day17】【微软 推出的SQLHelper】

从网络上找到 微软原版本的SQLHelper,很多行代码.认真看了,学习了. 代码: 1 using System; 2 using System.Data; 3 using System.Xml; 4 using System.Data.SqlClient; 5 using System.Collections; 6 7 namespace Helper 8 { 9 /// <summary> 10 /// The SqlHelper class is intended to encapsu

ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(一) 之 基层数据搭建,让数据活起来(数据获取)

大家好,本篇是接上一篇 ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(零) 前言  ASP.NET SignalR WebIM系列第二篇.本篇会带领大家将 LayIM界面中的数据动态化.当然还不涉及即时消息通讯,如果你已经搞定了数据界面,那么本文您可以简单的看一下,或者略过. 进入正题,layim帮我们定义好了数据规则,我们只要写一个接口实现那个json规范就可以了,剩下的事情就交给layim去做,看一下json格式.(对应文件夹:demo/json/getLi

微软原版SQLHelper类

C# Code 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

淘宝抓取数据

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using Stock.BLL;using System.Text.RegularExpressions;using System.Xml;using System.Data.SqlClient;using System.Web; namespace Stock.DAL{ public class

SSMS For Beginner Part 18 to 21

Part 18 Stored procedures in sql server Part 19 Stored procedures with output parameters Part 20 Stored procedure output parameters or return values Part 21 Advantages of stored procedures

csharp: Microsoft SqlHelper

from: Microsoft Data Access Application Block for .NET  https://www.microsoft.com/en-us/download/confirmation.aspx?id=435 // =============================================================================== // Microsoft Data Access Application Block fo

python 调用mysql存储过程返回结果集

存储过程: delimiter | create procedure get_product_info(in imid int(10),int iuser varchar(20)) begin select * from tb_test where mid = imid and user = iuser; end; | delimiter ; python调用: import mysql.connector cnx = mysql.connector.connect( user='test',p

Using Stored Programs with MySQLdb

http://flylib.com/books/en/1.142.1.125/1/ Using Stored Programs with MySQLdb The techniques for calling stored programs with MySQLdb differ only slightly from those for using traditional SQL statements. That is, we create a cursor, execute the SQL to

Identifying Duplicate Indexes

本文是在阅读<Troubleshooting SQL Server>->Chapter 5: Missing Indexes->Identifying Duplicate Indexes时,文中提及两个处理重复索引的链接.此处整理链接文章,方便自己后期查看,详细内容请参考原文:How can you tell if an index is REALLY a duplicate? and Removing duplicate indexes一.创建新的存储过程,返回表上的索引信息St