Память эффективный способ считать данные BLOB в 2005 C#/SQL

Причиной, что Вы делаете раннее связывание (строгий контроль типов), является производительность. С ранним связыванием Вы находите местоположение метода во время компиляции, так, чтобы во время выполнения это уже знало, где это живет.

Однако с поздним связыванием, необходимо пойти, ища метод, который походит на метод что клиентский названный код. И конечно, со многими, многими вызовами метода в программе, это - то, что делает динамические языки 'медленными'.

, Но уверенный, Вы могли создать статически скомпилированный язык, который делает позднее связывание, которое инвертировало бы многие преимущества статической компиляции.

6
задан OMG Ponies 28 September 2009 в 15:53
поделиться

2 ответа

See this excellent article here or this blog post for a long explanation how to do it.

Basically, you need to use a SqlDataReader and specify SequentialAccess to it when you create it - then you can read (or write) the BLOB from the database in chunks of whatever size is best for you.

Basically something like:

SqlDataReader myReader = getEmp.ExecuteReader(CommandBehavior.SequentialAccess);

while (myReader.Read())
{
   int startIndex = 0;

   // Read the bytes into outbyte[] and retain the number of bytes returned.
   retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);

   // Continue reading and writing while there are bytes beyond the size of the buffer.
   while (retval == bufferSize)
   {
      // write the buffer to the output, e.g. a file
      ....

      // Reposition the start index to the end of the last buffer and fill the buffer.
      startIndex += bufferSize;
      retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
   }

   // write the last buffer to the output, e.g. a file
   ....
}

// Close the reader and the connection.
myReader.Close();

Marc

4
ответ дан 17 December 2019 в 02:31
поделиться

The trick here is to use ExecuteReader in sequential mode, and read the data from the IDataReader. Here's a version for CLOBs - BLOBs are virtually identical, but with a byte[] and GetBytes(...).

Something like:

using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
    byte[] buffer = new byte[8040]; // or some multiple (sql server page size)
    while (reader.Read()) // each row
    {
        long dataOffset = 0, read;
        while ((read = reader.GetBytes(
            colIndex, dataOffset, buffer, 0, buffer.Length)) > 0)
        {
            // TODO: process "read"-many bytes from "buffer"
            dataOffset += read;
        }
    }
}
2
ответ дан 17 December 2019 в 02:31
поделиться
Другие вопросы по тегам:

Похожие вопросы: