SQL command in C#

Asked

Viewed 90 times

2

Well I’m doing a crud using mvc and Entity framework, only I don’t know how to pass parameters... here’s my code

public void Cadastrar(TimeModel timeObj)
{   
    strQuery = "INSERT INTO TimeFut (nome, estado) VALUES (aqui vai os parâmetros)";
    db.Database.ExecuteSqlCommand(strQuery);
} 

Someone could help me?

  • If you want to use "manual" commands, you do not need to use the Entity Framework. The good thing about using an ORM is that we don’t have to worry about "manual" CRUD. Take a look at some articles on CRUD + EF on the web and soon realize what I mean.

1 answer

6


You can add an object SqlParameter for every parameter you need.

To add parameters in the query just use @nomeDaVariavel, and in the SqlParameter you pass the parameter name without the arroba and the parameter value:

string nomeEstado = "Minas Gerais";
string query = @"INSERT INTO TimeFut (nome, estado) VALUES (@nome, @estado)";
db.Database.ExecuteSqlCommand(query, 
                              new SqlParameter("nome", "Pedro Paulo"),
                              new SqlParameter("estado", nomeEstado));
  • 2

    Very obg, it worked, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.