Class C # as parameter in SQL Server [closed]

3

asked by anonymous 12.04.2017 / 01:39

1 answer

4

The easy and simple way is using Dapper .

Running Stored Procedure for Selection

var cliente = connection.Query<Cliente>("PROC_Select_Cliente", new {ID = 1}, 
    commandType: CommandType.StoredProcedure).First();

Running Stored Procedure for Insertion

var parametros = new DynamicParameters();
parametros.Add("@ID", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue);
parametros.Add("@Nome", "Fulano");
parametros.Add("@Telefone", "(11) 2345-6789");

connection.Execute("PROC_Insert_Cliente", parametros, commandType: CommandType.StoredProcedure); 

int id = parametros.Get<int>("@ID");

Remembering that connection is any object that implements IDbConnection . It can be SqlConnection , for example.

Now, passing an object to a function that executes a Stored Procedure is a step a little further. I do not know if it's worth detailing in this answer.

    
12.04.2017 / 18:52