Solved

C# MySQL memorystream, blob, read

Posted on 2008-06-11
1
4,527 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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 new code templates in NetBeans IDE 8.0 for Windows.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now