yellowfever yellowfever - 1 year ago 518
C# Question

Return Values from query with stored procedure

I am trying to call a stored procedure

using Dapper.Net
and get return values.

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

I have tried a couple of different things with the parameter direction and using the
. If you step through the results, you can see that the proper return values are coming down in the
value, but I am not able to access the values the way it is described in the documentation as below..

Stored Procedures
Dapper supports fully stored procs:

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

Answer Source

You can read the value as below

var incidentId = retResults.ToList()[0].INCIDENT_ID; 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download