c# - 使用存储过程从 Dapper.net 查询返回值

标签 c# sql-server dapper

我正在尝试调用存储过程使用 Dapper.Net 并获取返回值。

p.Add("@INCIDENT_ID", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

var retResults = con.Execute("usp_GetIncidentID", p, commandType:CommandType.StoredProcedure);

int IncidentID = p.Get<int>("INCIDENT_ID"); 

我在参数方向和使用 "@INCIDENT_ID" 方面尝试了几种不同的方法。如果逐步查看结果,您会看到 retResults 值中出现了正确的返回值,但我无法按照下面文档中描述的方式访问这些值。 .

存储过程 Dapper 支持完全存储的过程:

var user = cnn.Query<User>("spGetUser", new {Id = 1}, 
    commandType: CommandType.StoredProcedure).First();}}}
If you want something more fancy, you can do:

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");   

最佳答案

怀疑(未经测试)这纯粹是您命名参数的方式不匹配;尝试(注意删除的 @):

p.Add("INCIDENT_ID", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);

var retResults = con.Execute("usp_GetIncidentID", p, commandType:CommandType.StoredProcedure);

int IncidentID = p.Get<int>("INCIDENT_ID"); 

关于c# - 使用存储过程从 Dapper.net 查询返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14247081/

相关文章:

c# - 如何快速将类实例保存/加载到文件

c# - WCF 并行模拟

javascript - 将图表宽度设置为 70%

sql-server - 如何从一个服务器连接切换到数据库引擎

c# - 将 session 从 ASP.net 存储在 redis 服务器中,同时使用 Response.redirect 重定向到其他页面时抛出错误

java - 从 Java 类内部调用存储过程并写入控制台?

c# - ASP.NET C# MySQL 查询执行失败时重试

javascript - 我的插入方法不起作用

c# - 使用 .NET 核心中的 dapper 批量插入 PostgreSQL

sql - Azure LogicApp 无法从 SQL Server 获取 rowid