c# – How to set size of a byte array dynamically?

Question:

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

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

I came 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);
}

Answer:

The current implementation of System.Array uses Int32 for all its built-in counters etc, so the theoretical maximum number of elements is Int32.MaxValue .

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

A good discussion and workaround here…

And some related questions and answers here…

Scroll to Top