SQL command in C #

0

Well, I'm doing a crud using mvc and the entity framework, but I do not 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);
} 

Could someone help me?

    
asked by anonymous 17.12.2018 / 17:50

1 answer

3

You can add a SqlParameter object for each parameter you need.

To add parameters to the query, simply use @nomeDaVariavel , and in SqlParameter you pass the parameter name without the at sign and 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));
    
17.12.2018 / 17:56