相关疑难解决方法(0)

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

我试图调用存储过程using 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"); 
Run Code Online (Sandbox Code Playgroud)

我已经尝试了几个与参数方向不同的东西并使用了"@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");   
Run Code Online (Sandbox Code Playgroud)

c# sql-server dapper

12
推荐指数
2
解决办法
3万
查看次数

从存储过程中获取输出参数的替代方法

我喜欢使用Dapper来满足我的ORM需求,但我知道必须有一种更好的方法来使用存储过程和强类型列表来插入/更新我的sql server数据库.

例如:

我有一首歌曲:

    public class Song
{
    public int Id { get; set; }
    public string title { get; set; }
    public string genre { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

并且有人提交歌曲列表:

 List<Song> songs = new List<Song> { 
            new Song { Id = 1, title = "Song 1" , genre="rock"}, 
            new Song { Id = 2, title = "Song 2" , genre="disco"}};
Run Code Online (Sandbox Code Playgroud)

我想使用我的存储过程更新数据库,该存储过程插入新歌曲或者如果歌曲已经存在则更新它.我的存储过程有两个输出参数: @success_added int = 0 and @success_updated int = 0

我的sproc如下:

ALTER PROCEDURE [dbo].[UpdateSong] 
@Id int = 0, 
@title …
Run Code Online (Sandbox Code Playgroud)

dapper

7
推荐指数
1
解决办法
1万
查看次数

标签 统计

dapper ×2

c# ×1

sql-server ×1