Generic Method of Finding, Using ADO.NET and Procedures

2

I'm looking for a solution from a generic repository for a DAL with a generic Find method, so there is no redundancy in my code. Using ADO.NET and Procedures.

I found something on this link:

Generic Subscription:

List Selecionar(IEnumerable operadores);

But I did not quite understand how to work with this list of operators.

Since I will use Procedure passing the parameters to my Procedure, is there any practical way for me to pass the operators to my Procedure in a generic way using ADO.NET?

    
asked by anonymous 24.09.2014 / 00:59

1 answer

2

Yes, it would actually work in a similar way.

Suppose the List Selecionar(IEnumerable operadores) method, requested as an example:

public override List<MeuObjeto> Selecionar(IEnumerable<Operadores.Operador> operadores)
{
    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        SqlCommand cmd = new SqlCommand("spMinhaSpDeSelecao", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        foreach (var operador in operadores)
        {
            cmd.Parameters.AddWithValue("@" + operador.Nome, operador.Valor);
        }

        SqlParameter valorSaida = new SqlParameter();
        valorSaida.ParameterName = "@ValorSaida";
        valorSaida.SqlDbType = System.Data.SqlDbType.Int;
        valorSaida.Direction = System.Data.ParameterDirection.Output;
        cmd.Parameters.Add(valorSaida);

        con.Open();
        var reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            yield return new MeuObjeto 
            {
                Campo1 = reader["Campo1"].ToString(),
                Campo2 = reader["Campo2"].ToString(),
                Campo3 = reader["Campo3"].ToString(),
                ...
            };
        }

        reader.Close();
    }
}
    
24.09.2014 / 01:13