c# - INSERT EXEC from DbCommand -
i'm trying execute results of stored procedure takes parameters temporary table.
// create #temptable // .. using (dbcommand command = connection.createcommand()) { command.commandtext = "insert #temptable exec [mystoredprocthathasparams]"; command.commandtype = commandtype.storedprocedure; command.parameters.add(someobject) command.executenonquery(); } output:
could not find stored procedure ''.
if remove command.commandtype = commandtype.storedprocedure, get:
procedure or function 'mystoredprocthathasparams' expects parameter '@p1' not supplied
is possible save output of stored procedure takes parameters query in c#?
the command type storedprocedure uses special, higher-performance method connecting sql server (an rpc call), requires command text exactly name of stored procedure. cannot include transact-sql in command text if want use commandtype.storedprocedure.
instead, need use commandtype.text , embed parameters sql string yourself:
cmd.commandtype = commandtype.text; cmd.commandtext = "insert #temptable exec [mystoredprocthathasparams] @param1, @param2"; cmd.parameters.add("@param1", sqldbtype.int).value = 1; cmd.parameters.add("@param2", sqldbtype.varchar, 100).value = "test";
Comments
Post a Comment