3
I’m developing an application that creates a arquivo.txt and saved in a column of my table in the database. The problem is in the query of that file.
What happens, when I want to do the query I have to have an array of bytes with defined size, to receive this data from the database that are in byte format. But I can’t dynamically define this size.
I even used Int32.Maxvalue, but it’s giving memory error.
Anyone can help?
Follows the code.
public static void GetArqTxtBD(int idColumBD, string pstrNomeArqTxt)
{
    using (SqlConnection conn = ConexaoBD.CriarConexao())
    {
        using (SqlCommand cmd = new SqlCommand("uspCtzTesteSelectArqtxtBd", conn))
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@idTesteSalvar", SqlDbType.Int).Value = idColumBD;
            FileStream fs;                                  // Escreve o BLOB para o arquivo (*.txt).
            BinaryWriter bw;                                // Define um Streams para o objeto 
            int tamanhoBuffer = Int32.MaxValue;             // Tamanho do buffer do BLOB
            byte[] byteSaida = new byte[tamanhoBuffer];     // o buffer BLOB byte[] para ser preenchido com GetBytes.
            long retorno;                                   // Os bytes retornados de GetBytes.
            long inicioIndice = 0;                          // A posicao inicial no BLOB de saida
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    // Cria o arquivo para tratar a saida dos dados
                    using (fs = new FileStream(pstrNomeArqTxt, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        using (bw = new BinaryWriter(fs))
                        {
                            // Reseta o byte de inicio para o novo BLOB.
                            inicioIndice = 0;
                            // Le os bytes no byteSaida[] e retem o numero de bytes retornados
                            retorno = reader.GetBytes(0, inicioIndice, byteSaida, 0, tamanhoBuffer);
                            // Continua lendo e escrevendo enquanto existir bytes ate completar o tamanho do buffer
                            while (retorno == tamanhoBuffer)
                            {
                                bw.Write(byteSaida);
                                bw.Flush();
                                //Reposiciona o inidice de inicio para o fim ultimo buffer e preenche o buffer
                                inicioIndice += tamanhoBuffer;
                                retorno = reader.GetBytes(0, inicioIndice, byteSaida, 0, tamanhoBuffer);
                            }
                            // Escreve o restante do buffer
                            bw.Write(byteSaida, 0, (int)retorno);
                            bw.Flush();
                        }
                    }
                }
            }
        }
    }
    Process.Start(pstrNomeArqTxt);
}
						
I was able to solve it, but I don’t know if it would be the best way. The code is the following: sizeBuffer = ((byte[])Reader.Getvalue(0)). Length; byteSaida = new byte[sizeBuffer]; In case now my array will have the size of the text file, and, there will be 2gb overflow of memory.
– Diego Farias