views:

423

answers:

2

I’m using the .NET entity framework and I’ve got one entity containing a varbinary. Is there an easy way to get the size of the varbinary in the codebehind, efter it’s been retrieved from the database?

I’m thinking there might be some way to get the size directly from the entity, something like entity.Context.Size – or do one need to handle it differently?

A: 

I solved it by running another query, getting the DATALENGTH() of the cell. Not the smoothest way but it works.

I'm still interested in hearing if anyone can come up with an answer for this though.

Marcus L
+2  A: 

A varbinary translates to a byte[] field in the entity framework, which means you can check the Length property of the array:

int fieldSize = entity.MyVarBinaryField.Length;

This works for me.

Bernhof