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.

4
  • try looking at the accepted answer here and do something like what's being done there.. stackoverflow.com/questions/11135245/… Commented May 31, 2013 at 18:50
  • 1
    Badabing! That was too damn easy. Thank Kraze. Commented May 31, 2013 at 18:58
  • sometimes the easiest things are easily overlooked.. glad I could help Commented May 31, 2013 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);? Commented Jan 16, 2021 at 11:27

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.