Solved

C# MySQL memorystream, blob, read

Posted on 2008-06-11
1
4,547 Views
Last Modified: 2013-12-17
I need to read an image file from a blob column in MySql database into a byte array that can be used as an image source in a WinForm. I do not know the size of the blob.
The code:
{
...
int id = 1;
UInt32 FileSize;
            byte[] rawData;
            IDbCommand dbcmd = dbconn.CreateCommand();
            string sql =
                       "SELECT image_file " +
                       "FROM current_session " +
                       "WHERE id = " + Id;
            dbcmd.CommandText = sql;
            IDataReader reader = dbcmd.ExecuteReader();
             reader.Read();  ...
}
At this point the reader should have the data.
How do I get that data to the rawData array?
0
Comment
Question by:eugene-g
1 Comment
 
LVL 25

Accepted Solution

by:
apeter earned 50 total points
ID: 21762107
Try Below. outByte will have your rawdata.

// Size of the BLOB buffer.
int bufferSize = 100;                  
// The BLOB byte[] buffer to be filled by GetBytes.
byte[] outByte = new byte[bufferSize];  
// The bytes returned from GetBytes.
long retval;                            
// The starting position in the BLOB output.
long startIndex = 0;

while (reader.Read())
{
  // Create a file to hold the output.
  stream = new FileStream(
    "logo" + pubID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
  writer = new BinaryWriter(stream);

  // Reset the starting byte for the new BLOB.
  startIndex = 0;

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

  // Continue while there are bytes beyond the size of the buffer.
  while (retval == bufferSize)
  {
    writer.Write(outByte);
    writer.Flush();

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

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question