Bug #45022 MySqlDataReader.GetBytes did not correctly return stream into Byte Array
Submitted: 22 May 2009 6:30 Modified: 19 Sep 2009 9:12
Reporter: Jon Li Email Updates:
Status: No Feedback Impact on me:
None 
Category:Connector / NET Severity:S2 (Serious)
Version:6.0.3 OS:Windows (XP Service Pack 2)
Assigned to: Assigned Account CPU Architecture:Any
Tags: Byte Stream, GetBytes, MySql Connector .NET, MySqlDataReader

[22 May 2009 6:30] Jon Li
Description:
Software:
-Microsoft Visual Studio 2005 Prosfessional Edition (ver. 8.0.50727.762) (Full Installation)
>ENU Service Pack 1 (kb 926601)
>Microsoft Visual Basic 2005
>Microsoft Visual Web Developer 2005

-MySQL Server 5.1 (Full Installation)

Add-Ons:
-MySQL Connector .NET 6.0.3

*Server:
Default Visual Studio debugging server running IIS

Language:
Visual Basic

Error Description:
Attempting to read the database mediumblob column using MySqlDataReader.GetBytes('+5 Valid Overloads'), returned error: 'GetBytes can only be called on binary columns'

Mediumblob column used to store binary image data which is inserted in after reading the ASP image upload component InputStream read in as byte(). Image data inserted exists, is valid and not corrupted.

Clarifications:
-Is BLOB (Binary Large Object) considered as a 'Binary' Column?

How to repeat:
Environment:
Essentially any Visual Studio 2005 Web Development Environment installed with described MySQL Connector add-on.

Create image table:
create table image (imgId int not null primary key  auto_increment, imgName varchar(50) not null default '', img mediumblob not null,  imgType varchar(25) not null default '', imgSize varchar(25) not null default '', imgCat varchar(25) not null default '') ENGINE = InnoDB;

Image file streamed into Byte using:
'Where file1 is the ASP.NET file upload component-
'ASP.NET VB Codes:
Dim imageBytes(File1.PostedFile.InputStream.Length) As Byte
File1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)

(After which the byte stream is inserted into the database by using a MySqlCommand.ExecuteNonQuery() command from the SQL DLL, which seemingly may not have been packaged with the actual MySQL .Net Connector but provides the same methods(in programming terms) to interact with the MySQL database.

The standalone DLL file can be found at http://blog.techsvr.com/?p=59)

.GetBytes method used to read stored data:
'Among the imports are:
'Imports mysql.data.MySqlClient

'The following code snippet is within an IF evaluation
'connection.Open() already executed before this snippet

Dim dr As MySqlDataReader = cmd.ExecuteReader()
    If dr.Read() Then
    'seq = dr.GetBytes(0, 0, Nothing, 0, dr.Item("imgSize")) - 1
    empPic = New Byte(dr.Item("imgSize")) {}
    dr.GetBytes(0, 0, empPic, 0, dr.Item("imgSize"))
    connection.Close()
End If

'Notably: The seq line of code commented out is supposed to be used to create 'the empPic by reading the image size as follows: 
seq = dr.GetBytes(0, 0, Nothing, 0, dr.Item("imgSize")) - 1
empPic = New Byte(seq) {}
'But presumably the dr.GetBytes() will return the same error

'Either way, I could not escape the problem for long. In order to stream the byte data back before assembling the image, this dr.GetBytes method was used to buffer the bytes in the empPic byte array:
dr.GetBytes(0, 0, empPic, 0, dr.Item("imgSize"))

'And then the error pops up: 
GetBytes can only be called on binary columns
'along with the same code being highlighted

Suggested fix:
No known workable fixes.

S4(Feature Request)
Could there be any way to implement a method that can read a BLOB column directly? Since they are very useful when used to store images or practically any kind of file.
[25 Jun 2009 2:26] Jon Li
Corrected to show OS Version
[19 Aug 2009 9:12] Tonci Grgin
Hi Jon and thanks for your report. Can you tell me what's wrong with http://dev.mysql.com/doc/refman/5.1/en/connector-net-programming-blob.html and did you try it at all?

As for BLOB's, they are BINARY but that does not mean GetBytes will work for them. LONGBLOB, for example, breaks Microsoft max_field_length barrier (2^32)...

Server version: 5.1.31-log MySQL Community Server (GPL)

mysql> create table bug45022(Id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY
, ImgData BLOB);
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO bug45022 (id) VALUES(NULL);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM bug45022;
Field   1:  `Id`
Catalog:    `def`
Database:   `test`
Table:      `bug45022`
Org_table:  `bug45022`
Type:       LONG
Collation:  binary (63)
Length:     10
Max_length: 1
Decimals:   0
Flags:      NOT_NULL PRI_KEY UNSIGNED AUTO_INCREMENT NUM PART_KEY

Field   2:  `ImgData`
Catalog:    `def`
Database:   `test`
Table:      `bug45022`
Org_table:  `bug45022`
Type:       BLOB
Collation:  binary (63)
Length:     65535
Max_length: 0
Decimals:   0
Flags:      BLOB BINARY

+----+---------+
| Id | ImgData |
+----+---------+
|  1 | NULL    |
+----+---------+
1 row in set (0.00 sec)

mysql>
[19 Sep 2009 23:00] Bugs System
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".