c# - Execute stored procedure w/parameters in Dapper -
i'm using dapper (thanks sam, great project.) micro orm dal , reason i'm not able execute stored procedures input parameters.
in service i've got this:
public void getsomething(int somethingid) { irepository<something, somethingenum> repository = unitofwork.getrepository<something, somethingenum>(); var param = new dynamicparameters(); param.add("@somethingid", dbtype: dbtype.int32, value:somethingid, direction: parameterdirection.input); var result = repository.exec<something>(somethingenum.spmystoredprocedure, param); ... }
when execution of stored procedure happens sqlexception thrown stating need provide 'somethingid'
procedure or function 'spmystoredprocedure' expects parameter '@somethingid', not supplied.
my dal similar based on github project of pencroff.
am missing here?
update: passing commandtype via somethingenum:
public class somethingenum : enumbase<somethingenum, string> { public static readonly somethingenum spmystoredprocedure = new somethingenum("spmystoredprocedure", "[dbo].[spmystoredprocedure]", commandtype.storedprocedure); public somethingenum(string name, string enumvalue, commandtype? cmdtype): base(name, enumvalue, cmdtype) { } }
you need tell command type: make sure there's commandtype: commandtype.storedprocedure
in dapper call. otherwise, executing text command:
spmystoredprocedure
(with unused parameters in ambient context). legal tsql, , attempts call spmystoredprocedure
without passing parameters - same if put spmystoredprocedure
ssms , press f5.
also, if parameters fixed, suggest using:
var param = new { somethingid };
or inline completely:
var result = repository.exec<something>(somethingenum.spmystoredprocedure, new { somethingid }, commandtype: commandtype.storedprocedure);
(note: if exec<t>
method ever handles stored procedures, move commandtype
internal method - or make optional parameter defaults commandtype.storedprocedure
)
Comments
Post a Comment