博观而约取,厚积而薄发。这篇文章主要讲述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 = https://www.songbingjia.com/android/6
}
Method 1 Use ParameterDirection.ReturnValue key:
【Dapper:How to get return value ( output value) by call stored procedure】return @@IDENTITYMyTabel:
p.Add(" @ID" , dbType: DbType.Int32, direction:ParameterDirection.ReturnValue);
var id = p.Get(" @tID" );
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 INTINSERT 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 INTINSERT 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();
}
推荐阅读
- 一篇文章带你领略Android混淆的魅力
- 使用电脑热点对Android app进行抓包
- Could not delete D:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/platfor
- Android5_了解4大组件
- Mac下Appnium的Android的UI自动化环境搭建
- Android Studio获取调试版SHA1和发布版SHA1的方法
- uniapp属性插值报错Interpolation inside attributes has been removed. Use v-bind or the colon shorthand inst
- Rocket - regmapper - RegField
- 综述(Active Shape Models-Their Training and Application——1995)