With the output argument, run SQLCommand.

asp.net-mvc asp.net-mvc-3 entity-framework

Question

I'm attempting to utilize the following code in an ASP.NET MVC3 application that uses Entity Framework:

var token = "";
this.Database.ExecuteSqlCommand("exec dbo.MyUsp", new SqlParameter("token", token));

The stored-proc signature I use is:

CREATE PROCEDURE MyUSP(@token varchar(10) OUT)
(...)

A message stating that the argument "@token" was anticipated but not given appears when I use this code.

How can I specify that EF should use the token argument for output?

1
19
7/28/2011 3:40:36 PM

Accepted Answer

I had to use this in the end, but there must be a better approach:

var p = new SqlParameter
{
    ParameterName = "token",
    DbType = System.Data.DbType.String,
    Size = 100,
    Direction = System.Data.ParameterDirection.Output
};
var resp = this.Database.SqlQuery<String>("exec dbo.usp_GetRequestToken @token", p);

return resp.First();
25
7/7/2015 3:20:36 PM

Popular Answer

var outParam = new SqlParameter();
outParam.ParameterName = "OutPutParametname";
outParam.SqlDbType = SqlDbType.Bit;//DataType Of OutPut Parameter
outParam.Direction = ParameterDirection.Output;
db.Database.ExecuteSqlCommand("EXEC ProcedureName @Param1,@Param2 OUTPUT", new SqlParameter("Param1", value), outParam);
object outParamValue = Convert.ToBoolean(outParam.Value);


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow