Benjamin RD
Benjamin RD

Reputation: 12044

Linq Stored procedures, send parameter via System.Data.CommandType.StoredProcedure

I have a stored procedure and I need to send some parameters, just now I have:

readonly efacEntities _db = new efacEntities();

This is my model instance

And into my function I have:

_db.Database.Connection.Open();

var command = _db.Database.Connection.CreateCommand();
command.CommandText = "dbo.MyStoredProceadure ";
command.CommandType = System.Data.CommandType.StoredProcedure;

command.Parameters["@step"].Value = "668987";
command.CommandType = System.Data.CommandType.StoredProcedure;

var test = (command.ExecuteScalar());

But I get an error.

Is necessary send the parameter step or the parameter is not executing the command.ExecuteScalar, how I can send parameters in this query type?

Upvotes: 0

Views: 428

Answers (1)

Giorgos Betsos
Giorgos Betsos

Reputation: 72195

Try this to correctly add the parameter to the command object prior to executing it:

command.Parameters.Add("@step", SqlDbType.Int).Value = 668987;

if @step is an int, or:

command.Parameters.Add("@step", SqlDbType.VarChar, 50).Value = "668987";

if @step is VARCHAR(50)

EDIT:

The above statements work provided that the command object is of type SqlCommand, which is clearly not the case here!

For a DbCommand object we can add a new parameter like this:

command.Parameters.Add(new SqlParameter() 
                       { ParameterName = "@step", 
                         SqlDbType = SqlDbType.Int, 
                         SqlValue = "66897" 
                       })

Upvotes: 1

Related Questions