How to return multiple rows with byte array from sql Server

1

I need to make a query in a table where the data is of type varbinary(max) , so I created the method below:

public List<byte[]> preenche_fotos(string nCrm)
    {
        consql.bd_string();
        SqlConnection sqlconn = new SqlConnection(consql.sqlconn);

        List<byte[]> list_foto = new List<byte[]>();

        try
        {
            consql._sql = @"SELECT bfoto
                            FROM crm_fotos
                            WHERE ncrm = @nCrm";

            SqlCommand cmd = new SqlCommand(consql._sql, sqlconn);

            cmd.Parameters.Add("@nCrm", SqlDbType.VarChar).Value = nCrm;

            sqlconn.Open();

            SqlDataReader leitor = cmd.ExecuteReader();

            while (leitor.Read())
            {
                list_foto.Add(???????);
            }
        }
        catch (Exception error)
        {
            MessageBox.Show("Error" + error);
        }
        finally
        {
            sqlconn.Close();
        }

        return list_foto;
    } 
}

So, with the method Read of DataReader , I'm reading line the query line, but how do I add the value to list_foto ?

    
asked by anonymous 11.05.2017 / 02:32

1 answer

2

To pass the values would be cast to array de bytes ( byte[] ) as follows:

while (leitor.Read())
{
    list_foto.Add((byte[])leitor['bfoto']);
}

11.05.2017 / 02:34