PetaPoco and output parameters from stored procedures?

不问归期 提交于 2019-12-12 13:04:39

问题


I'm trying to setup an output parameter using PetaPoco. I found someone using this sample online:

var ctx = new CustomDBDatabase();
var total = new SqlParameter("Total", System.Data.SqlDbType.Int);
total.Direction = System.Data.ParameterDirection.Output;

var results = ctx.Query<DBEntity>("exec GetDBEntities @StartIndex, @MaxIndex, @TotalCount = @Total out", 
  id, start, max, total);

int totalCount = (int)total.Value;

However, total.value returns null, even though when I run this statement directly against SQL Server, it returns me 3. Is this setup correctly with PetaPoco? Are output parameters supported?

Thanks.


回答1:


This is supported. But your current syntax is wrong anyways.

var ctx = new CustomDBDatabase();
var total = new SqlParameter("TotalCount", System.Data.SqlDbType.Int);
total.Direction = System.Data.ParameterDirection.Output;

var results = ctx.Query<DBEntity>("exec GetDBEntities @StartIndex, @MaxIndex, @TotalCount OUTPUT", new { StartIndex = start, MaxIndex = max, TotalCount = total});

int totalCount = (int)total.Value;

Something like this should work though. Not quite sure of the sql syntax but this should get you on your way.



来源:https://stackoverflow.com/questions/8612167/petapoco-and-output-parameters-from-stored-procedures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!