Álvaro García
Álvaro García

Reputation: 19356

why doesnt' it use the parameter in this raw sql?

I am using this code:

SqlParameter pNombreUsuario = new SqlParameter("NombreUsuario", paramNombreUsuario);
object[] parametros = new object[] { pNombreUsuario };

string passwordDB = dbContext.Database.SqlQuery<string>("select password from Personal where NombreUsuario = @NombreUsuario", parametros)
                             .SingleOrDefault<string>();

but the query that is sent to the database is:

select password from Personal where NombreUsuario = @NombreUsuario

Why the parameter with the username name is not used?

Thanks.

Upvotes: 0

Views: 40

Answers (1)

Dean Ward
Dean Ward

Reputation: 4783

The docs indicate that you should send an array of object values rather than SqlParameter objects.

E.g.

string passwordDB = dbContext.Database
                             .SqlQuery<string>("select password from Personal where NombreUsuario = @p0", paramNombreUsuario)
                             .SingleOrDefault<string>();

Does that work for you?

Edit: I misread the docs. Try this:

string passwordDB = dbContext.Database
                             .SqlQuery<string>(
                                 "select password from Personal where NombreUsuario = @NombreUsuario", 
                                 new SqlParameter("NombreUsuario", paramNombreUsuario))
                             .SingleOrDefault<string>();

Upvotes: 1

Related Questions