Save a byte array to sql from a Datatablereader

Asked

Viewed 205 times

0

In one of the system tasks, I need to query an Sql Server database, which returns a Datatable, in this Datatable, one of the data is of the type Byte[]

So to read the Datatable use a DataTableReader, that generates me a Object, right?

 async private void Upload_Ra(DataTable Dat_RaWebLocal)
    {
        DataTableReader dtr = Dat_RaWebLocal.CreateDataReader();

        if(dtr.HasRows)
        {
            textBox2.Text = "Relatorio encontrado, iniciando enviado para nuvem";

            Classes.Cadastro.Crm.Upload_RaWeb Up_RaWeb = new Classes.Cadastro.Crm.Upload_RaWeb();

            await dtr.ReadAsync();

            Up_RaWeb.Upload(Convert.ToInt32(dtr["id"]), dtr["cod_item_Crm"].ToString(), Convert.ToInt32(dtr["cod_cli"]), dtr["razao_social"].ToString(), Convert.ToDateTime(dtr["data_rec"])
                            , Convert.ToDateTime(dtr["data_anal"]), Convert.ToInt32(dtr["n_Crm"]), Convert.ToInt32(dtr["n_Nf"])
                            , dtr["OF_anterior"].ToString(), dtr["modelo"].ToString(), dtr["material"].ToString(), dtr["arranjo"].ToString(), dtr["diam"].ToString()
                            , dtr["tipo"].ToString(), dtr["inicio_op"].ToString()
                            , dtr["fim_op"].ToString(), dtr["motivo"].ToString(), dtr["tag"].ToString(), dtr["fabric_bomba"].ToString(), dtr["mod_bomba"].ToString()
                            , dtr["prod_fluido"].ToString(), Convert.ToDecimal(dtr["temp_fluido"])
                            , Convert.ToDecimal(dtr["rpm"]), dtr["api"].ToString(), Convert.ToDecimal(dtr["pres_suc"]), Convert.ToDecimal(dtr["pres_desc"])
                            , dtr["diag_falha"].ToString(), dtr["analise"].ToString(), dtr["conclusao"].ToString()
                            , dtr["recomend"].ToString(), Convert.ToInt32(dtr["nivel"]), ObjectToByteArray(dtr["bfoto1"]), "", ObjectToByteArray(dtr["bfoto2"]), ""
                            , ObjectToByteArray(dtr["bfoto3"]), "", ObjectToByteArray(dtr["bfoto4"]), "", ObjectToByteArray(dtr["bfoto5"]), "", ObjectToByteArray(dtr["bfoto6"]), "");                
        }
    }

So I need to convert this Object for Byte[] so you can save to another server.

So, researching, I found the following question:

Convert any Object to a byte[]

Then, using the method below:

byte[] ObjectToByteArray(object obj)
{
    if(obj == null)
        return null;
    BinaryFormatter bf = new BinaryFormatter();
    using (MemoryStream ms = new MemoryStream())
    {
        bf.Serialize(ms, obj);
        return ms.ToArray();
    }
}

I was able to convert the Object for byte[], but, I noticed that the same totally changed the string, this certain?

There’s another way I can get that array ?

  • 1

    The dtr["bfoto3"] its value is not a array de bytes if yes no need to convert again to array de bytes just give one cast in that variable!

  • @Virgilionovic Perfect. Thank you

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.