使用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 2 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的更多相关文章

  1. [转]How to get return values and output values from a stored procedure with EF Core?

    本文转自:https://stackoverflow.com/questions/43935345/how-to-get-return-values-and-output-values-from-a- ...

  2. SQL Server存储过程Return、output参数及使用技巧

    SQL Server目前正日益成为WindowNT操作系统上面最为重要的一种数据库管理系统,随着 SQL Server2000的推出,微软的这种数据库服务系统真正地实现了在WindowsNT/2000 ...

  3. return value, output parameter,

    Return Value https://docs.microsoft.com/en-us/sql/t-sql/language-elements/return-transact-sql?view=s ...

  4. [转]Easy Stored Procedure Output Oracle Select

    本文转自:http://www.oraclealchemist.com/oracle/easy-stored-procedure-output/ I answered a question on a ...

  5. EF 接收OUTPUT参数的方法 How to Retrieve Stored Procedure Output Parameters in Entity Framework

    原文地址:http://blogs.microsoft.co.il/gilf/2010/05/09/how-to-retrieve-stored-procedure-output-parameters ...

  6. Dapper 的输出参数使用示范

    -- 普通SQL 示范-- Queries with output parameters. Hide Shrink Copy Code // output parameters // the para ...

  7. 微软版的SqlHelper.cs类

    一,微软SQLHelper.cs类 中文版: using System; using System.Data; using System.Xml; using System.Data.SqlClien ...

  8. OracleHelper类

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

  9. SqlHelper类

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

随机推荐

  1. Chrome崩溃的解决办法

    前两天Win10 更新的安全组件,第二天上班来就打开不了Chrome了,打开就是:噢哟,崩溃了! 那是连 setting 页都打不开的啊...好晕,好晕. 我是真的有点崩溃啊,在网上找了好久,什么与百 ...

  2. 怎么删除iOS模拟器上的应用程序?

    怎么删除iOS模拟器上的应用程序: 和手机上一样,鼠标长按,点击删除 xcode 卸载模拟器 Simulator:删除目录/Library/Developer/CoreSimulator/Profil ...

  3. Mysql 学习参考

    [1]Mysql 基础知识 (1)<Mysql 官网> (2)<菜鸟教程之Mysql数据库教程> (3)<C语言中文网之Mysql数据库栏> (4)<W3Sc ...

  4. 在C++中调用FFTW

    FFTW是一个可以进行可变长度一维或多维DFT的开源C程序库,是目前最快的FFT算法实现. 本文简述了在Windows平台上,如何在C++中调用FFTW,所使用的IDE为Visual Studio 2 ...

  5. go ---switch语句

    package main import ( "fmt" ) func main() { var ar = [...]string{"A", "B&qu ...

  6. GoldenDict(for Linux)配置无道词典

    引言 我原来写过一篇博客:(离线)英语词典软件推荐,个人比较喜欢的就是GoldenDict词典.不仅仅是因为它是免费开源的多平台程序,更重要的是支持丰富的原版词典(下文给出了下载链接).本文主要针对其 ...

  7. (原创)使用C#开发PLC上位机监控系统客户端应用程序

    PLC客户端监控系统的特点: 0.客户端系统软件可部署在 多个管理层的PC机上,或者需要部署在距离服务器较远区域的PC机上,通过网线连接到服务器端的交换机. 1应用范围: (1)所有客户端都只有监视功 ...

  8. SpringMVC中的400错误,The request sent by the client was syntactically incorrect.

    在其他对象属性类型一样情况下,只需要创建一个类,再在springmvc.xml中添加配置: package com.ujiuye.common; import org.springframework. ...

  9. PyTorch 安装 报错,原因是pip 不是64位的。

    原因: import pip._internal print(pip._internal.pep425tags.get_supported()) 换位64位的python版本. import pip. ...

  10. MAC电脑下Appium + python3 + robotframework ios的真机测试环境搭建

    本人的环境搭建前的准备,MAC电脑一台(macOS Mojave 10.14.0及以上),Xcode 10.0及以上   ,自己注册的一个Apple ID 账户,必须你的电脑能连接互联网,最好不要用公 ...