How to return a DataReader C #

0

I know I'm doing it the wrong way.

However I would like to know if it is possible within what I am trying to return a dataReader.

asked by anonymous 23.10.2017 / 18:26

1 answer

3
Look, I do not see how you could do this safely. If you use using , DataReader will be closed, if you do not use using , the connection will remain open until someone closes it.

An alternative, is to pass a Action<​Db​Data​Reader> and do all necessary operations on it.

public static List<Cliente> BuscarCliente(int id, Function<NpgsqlDataReader, Cliente> callback) 
{
    var clientes = new List<Cliente>();
    using (conn = new NpgsqlConnection(ConnString))
    {
        conn.Open();
        string cmdBuscar = String.Format("SELECT * FROM CLIENTES WHERE id_cliente = @ID");
        using (NpgsqlCommand cmd = new NpgsqlCommand(cmdBuscar, conn))
        {
            cmd.Parameters.Add(new NpgsqlParameter("@ID", id));
            using (NpgsqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    var cliente = callback(reader);
                    clientes.Add(cliente);
                }
            }
        }
    }
    return clientes;
}

public static Main()
{
    var idExterno = Guid.NewGuid();
    BuscarCliente(123, (reader) => {
        var cliente = new Cliente();
        cliente.IdExterno = idExterno;
        cliente.Prop1 = reader[0].ToString();
        cliente.Prop2 = reader[1].ToString();
        cliente.Prop3 = reader[2].ToString();
        cliente.Prop4 = reader[3].ToString();
        cliente.Prop5 = reader[4].ToString();
        cliente.Prop6 = reader[5].ToString();
        return cliente;
    });
}

You can even make the above method more general by reading the columns of the DbDataReader header, dynamically mounting the return, etc.

As I see it, this approach is far from Recommended. If you can, I advise you to use at least Dapper , or better Dapper + Dommel .

EDIT

In your specific case, the code below should suffice.

public void BuscarCliente(int id, Action<NpgsqlDataReader> callback) 
{
    using (conn = new NpgsqlConnection(ConnString))
    {
        conn.Open();
        string cmdBuscar = String.Format("SELECT * FROM CLIENTES WHERE id_cliente = @ID");
        using (NpgsqlCommand cmd = new NpgsqlCommand(cmdBuscar, conn))
        {
            cmd.Parameters.Add(new NpgsqlParameter("@ID", id));
            using (NpgsqlDataReader reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    callback(reader);
                }
            }
        }
    }
}

private void btnBuscarCliente_Click(object sender, EventArgs e)
{
    var id = Convert.ToInt32(txtBuscarCliente.Text);
    BuscarCliente(id, (reader) => {
        txtId.Text = reader[0].ToString();
        txtNomeCliente.Text = reader[1].ToString();
        txtEndereco.Text = reader[2].ToString();
        txtCidade.Text = reader[3].ToString();
        txtEmail.Text = reader[4].ToString();
        txtTelefone.Text = reader[5].ToString();
    });
}
    
23.10.2017 / 18:43