Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExecuteSqlCommand with output parameter

Tags:

I'm using Entity Framework in an ASP.NET MVC3 application and I'm trying to use the following code:

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

My stored proc signature is:

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

When I use this code I get an error saying that parameter "@token" was expected but not supplied.

How do I tell EF that the token parameter is for output?

like image 308
Sam Huggill Avatar asked Jul 28 '11 15:07

Sam Huggill


People also ask

Can we call stored procedure in Entity Framework Core?

You can execute SP using FromSql method in EF Core in the same way as above, as shown below.


1 Answers

I ended up using this to get it working, but I'm sure there's a more optimal way:

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(); 
like image 151
Sam Huggill Avatar answered Sep 20 '22 19:09

Sam Huggill