c# - How can I specify parameters when calling a stored procedure with Database.ExecuteSqlCommand -
c# - How can I specify parameters when calling a stored procedure with Database.ExecuteSqlCommand -
i trying following:
var sql = @"dbo.sp_ins_test, @title, @examid, @topicid"; list<sqlparameter> parameterlist = new list<sqlparameter>(); parameterlist.add(new sqlparameter ("title", "sample test: " + testnumber)); parameterlist.add(new sqlparameter ("examid", examid)); parameterlist.add(new sqlparameter ("topicid", testtopicid)); sqlparameter[] parameters = parameterlist.toarray(); int result = db.database.executesqlcommand(sql, parameters); but gives error:
stacktrace= @ system.data.sqlclient.sqlconnection.onerror(sqlexception exception, boolean breakconnection, action`1 wrapcloseinaction) @ system.data.sqlclient.tdsparser.throwexceptionandwarning(tdsparserstateobject stateobj, boolean callerhasconnectionlock, boolean asyncclose) @ system.data.sqlclient.tdsparser.tryrun(runbehavior runbehavior, sqlcommand cmdhandler, sqldatareader datastream, bulkcopysimpleresultset bulkcopyhandler, tdsparserstateobject stateobj, boolean& dataready) @ system.data.sqlclient.sqlcommand.finishexecutereader(sqldatareader ds, runbehavior runbehavior, string resetoptionsstring) @ system.data.sqlclient.sqlcommand.runexecutereadertds(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, boolean async, int32 timeout, task& task, boolean asyncwrite, sqldatareader ds) @ system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, string method, taskcompletionsource`1 completion, int32 timeout, task& task, boolean asyncwrite) @ system.data.sqlclient.sqlcommand.internalexecutenonquery(taskcompletionsource`1 completion, string methodname, boolean sendtopipe, int32 timeout, boolean asyncwrite) @ system.data.sqlclient.sqlcommand.executenonquery() @ system.data.entity.infrastructure.interception.dbcommanddispatcher.<nonquery>b__0(dbcommand t, dbcommandinterceptioncontext`1 c) @ system.data.entity.infrastructure.interception.internaldispatcher`1.dispatch[ttarget,tinterceptioncontext,tresult](ttarget target, func`3 operation, tinterceptioncontext interceptioncontext, action`3 executing, action`3 executed) @ system.data.entity.infrastructure.interception.dbcommanddispatcher.nonquery(dbcommand command, dbcommandinterceptioncontext interceptioncontext) @ system.data.entity.internal.interceptabledbcommand.executenonquery() @ system.data.entity.core.objects.objectcontext.<>c__displayclass59.<executestorecommand>b__58() @ system.data.entity.core.objects.objectcontext.executeintransaction[t](func`1 func, idbexecutionstrategy executionstrategy, boolean startlocaltransaction, boolean releaseconnectiononsuccess) @ system.data.entity.core.objects.objectcontext.<>c__displayclass59.<executestorecommand>b__57() @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func`1 operation) @ system.data.entity.core.objects.objectcontext.executestorecommand(transactionalbehavior transactionalbehavior, string commandtext, object[] parameters) @ system.data.entity.internal.internalcontext.executesqlcommand(transactionalbehavior transactionalbehavior, string sql, object[] parameters) @ system.data.entity.database.executesqlcommand(transactionalbehavior transactionalbehavior, string sql, object[] parameters) @ system.data.entity.database.executesqlcommand(string sql, object[] parameters) @ webrole1.controllers.testcontroller.<post>d__1d.movenext() in c:\k\ s362\webrole1\controllers\web api - data\testcontroller.cs:line 186 --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ system.threading.tasks.taskhelpersextensions.<casttoobject>d__3`1.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ system.web.http.controllers.apicontrolleractioninvoker.<invokeactionasynccore>d__0.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ system.web.http.filters.actionfilterattribute.<callonactionexecutedasync>d__5.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.exceptionservices.exceptiondispatchinfo.throw() @ system.web.http.filters.actionfilterattribute.<callonactionexecutedasync>d__5.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ system.web.http.filters.actionfilterattribute.<executeactionfilterasynccore>d__0.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ system.web.http.controllers.actionfilterresult.<executeasync>d__2.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ system.web.http.controllers.authenticationfilterresult.<executeasync>d__0.movenext() --- end of stack trace previous location exception thrown --- @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task) @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task) @ system.runtime.compilerservices.taskawaiter`1.getresult() @ system.web.http.dispatcher.httpcontrollerdispatcher.<sendasync>d__0.movenext() can confirm if right way phone call stored procedure parameters. error not seem clear rather confused.
the issue comma. remove it:
var sql = @"dbo.sp_ins_test @title, @examid, @topicid"; while you're there, create const string instead of var, string-interning won't matter much. compiler might you.
c# asp.net sql-server
Comments
Post a Comment