Save a byte array in the sql from a Datatablereader

0

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

So, to read Datatable I use a DataTableReader , which gives 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 to Byte[] so you can save to another server.

So by searching, 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 Object to byte[] , but I noticed that it totally changed the string, is that right?

Is there another way, that I can recover this array ?

    
asked by anonymous 19.07.2017 / 22:26

0 answers