0

I have been have a bit of an issue with MySqlDataReader.GetBytes, and referencing the column ordinal.

Table structure:

ID - Primary, int, not null, auto_increment
TNode - , not null
Packet - longblob
Timestamp - timestamp

If I run the following query:

SELECT * FROM table WHERE TNode = 2;

And attempt to get the size of the longblob using the following method:

while (reader.Read()) 
{
     long l = reader.GetBytes(2,0,null,0,0);
} 

I will receive the following error:

GetBytes can only be called on binary or guid columns

Which, column index 2 is. Even if I iterate through all of the columns, as expected, I receive the same error. But if I run the following query:

SELECT Packet, ID, Timestamp FROM table WHERE TNode = 2;

Followed by:

while (reader.Read()) 
{
     long l = reader.GetBytes(0, 0, null, 0, 0);
} 

No problems. I am able to get the length of the longblob, and do what I need to do with it.

Any ideas as to why it is not allowing me to use a non-zero column index?

Thank you.

nawfal
  • 70,104
  • 56
  • 326
  • 368
  • try looking at the accepted answer here and do something like what's being done there.. http://stackoverflow.com/questions/11135245/what-length-parameter-should-i-pass-to-sqldatareader-getbytes – MethodMan May 31 '13 at 18:50
  • 1
    Badabing! That was too damn easy. Thank Kraze. – Hammerzeit May 31 '13 at 18:58
  • sometimes the easiest things are easily overlooked.. glad I could help – MethodMan May 31 '13 at 21:19
  • interesting, what if there are several BLOB columns returned by query? How then this would work long l = reader.GetBytes(0, 0, null, 0, 0);? – Oleksandr Jan 16 '21 at 11:27

1 Answers1

0

Not sure about MySQL, but in SQL Server SELECT * might not return the columns in the order you expect. It might NOT be column 2. In fact, if you change the table and continue to use SELECT * you might introduce a hard-to-find bug. I'd suggest specifying the columns as you did in your test.

n8wrl
  • 19,439
  • 4
  • 63
  • 103
  • Yeah, I get that. Its weird though, even if the wild card select doesn't return the columns in the expected order, prior to running a GetBytes, if I iterate through the columns and get the index of the Packet column, it still fails if the index greater than 0. – Hammerzeit May 31 '13 at 18:49