How to set a byte array size dynamically?

3

I'm developing an application that creates a arquivo.txt and saves it to a column of my table in the database. The problem is in querying this file.

What happens, when I want to make the query I have to have an array of bytes with size defined, to receive this data from the database that are in bytes format. But I'm not able to set this size dynamically.

I got to use Int32.MaxValue, but it is giving memory error.

Can anyone help?

Follow 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);
}
    
asked by anonymous 26.08.2016 / 16:15

1 answer

2

The current implementation of System.Array use Int32 for all its internal counters etc, then the theoretical maximum number of elements is Int32.MaxValue .

But the .NET framework also has a maximum limit of 2GB object size, enforced by Microsoft CLR.

A good discussion and workaround here ...

And some related, questions and answers here ...

29.08.2016 / 19:36