MySql.Data.MySqlClient.MySqlDataReader.GetBytes C# (CSharp) Method

GetBytes() public method

Reads a stream of bytes from the specified column offset into the buffer an array starting at the given buffer offset.
public GetBytes ( int i, long fieldOffset, byte buffer, int bufferoffset, int length ) : long
i int The zero-based column ordinal.
fieldOffset long The index within the field from which to begin the read operation.
buffer byte The buffer into which to read the stream of bytes.
bufferoffset int The index for buffer to begin the read operation.
length int The maximum length to copy into the buffer.
return long
    public override long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
    {
      if (i >= FieldCount)
        Throw(new IndexOutOfRangeException());

      IMySqlValue val = GetFieldValue(i, false);

      if (!(val is MySqlBinary) && !(val is MySqlGuid))
        Throw(new MySqlException("GetBytes can only be called on binary or guid columns"));

      byte[] bytes = null;
      if (val is MySqlBinary)
        bytes = ((MySqlBinary)val).Value;
      else
        bytes = ((MySqlGuid)val).Bytes;

      if (buffer == null)
        return bytes.Length;

      if (bufferoffset >= buffer.Length || bufferoffset < 0)
        Throw(new IndexOutOfRangeException("Buffer index must be a valid index in buffer"));
      if (buffer.Length < (bufferoffset + length))
        Throw(new ArgumentException("Buffer is not large enough to hold the requested data"));
      if (fieldOffset < 0 ||
        ((ulong)fieldOffset >= (ulong)bytes.Length && (ulong)bytes.Length > 0))
        Throw(new IndexOutOfRangeException("Data index must be a valid index in the field"));

      // adjust the length so we don't run off the end
      if ((ulong)bytes.Length < (ulong)(fieldOffset + length))
      {
        length = (int)((ulong)bytes.Length - (ulong)fieldOffset);
      }

      Buffer.BlockCopy(bytes, (int)fieldOffset, buffer, (int)bufferoffset, (int)length);

      return length;
    }

Usage Example

Ejemplo n.º 1
0
        public static byte[] convertBlobToBufferData(String column,MySqlDataReader rdr)
        {
            int bufferSize = 1024; // Number of bytes to read at a time
            byte[] ImageData = new byte[bufferSize];
            long nBytesReturned, startIndex = 0;
            int ordinal = rdr.GetOrdinal(column);
            string image = rdr.IsDBNull(ordinal) ? null : rdr.GetString(column);
            if (image != null)
            {
                startIndex = 0;

                nBytesReturned = rdr.GetBytes(
                ordinal, // Column index of BLOB column
                startIndex, // Start position of the byte to read
                ImageData, // Byte array to recieve BLOB data
                0, // Start index of the array
                bufferSize // Size of buffer
                );
                while (nBytesReturned == bufferSize)
                {
                    startIndex += bufferSize;
                    nBytesReturned = rdr.GetBytes(ordinal, startIndex, ImageData, 0, bufferSize); // Number of bytes returned is assigned to nBytesReturned
                }
                return ImageData;
            }
            else
            {
                return null;
            }
        }
All Usage Examples Of MySql.Data.MySqlClient.MySqlDataReader::GetBytes