Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binary Blob truncated to 8000 bytes - SQL Server 2008 / varbinary(max)

Tags:

I have upgraded from Fluent Nhibernate 1.0 with Nhibernate 2.1 to pre- release 1.x with NHibernate 3.0 GA and have hit what I think is a regression, but I want to hear if that's indeed the case.

I am using SQL Server Express 2008 and the MSSQL 2008 dialect and have an Image property of type System.Drawing.Image and I have mapped it like this:

Map (food => food.Image)  .Length (int.MaxValue)  .Nullable (); 

The Image column in the table is of type varbinary(MAX).

The generated hbm for the property is:

<property name="Image" type="System.Drawing.Image, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">    <column name="Image" length="2147483647" not-null="false" /> </property>` 

However no matter what I do the binary blob is truncated to 8000 bytes when serialized with the current FNH and NH versions. That didn't used to be the case with previous versions.

Ideas of why this is happening and how to fix/workaround it?

like image 557
Ivan Zlatev Avatar asked Jan 03 '11 11:01

Ivan Zlatev


People also ask

What is the max size of VARBINARY in SQL Server?

varbinary [ ( n | max) ] Variable-length binary data. n can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes. The storage size is the actual length of the data entered + 2 bytes.

What does VARBINARY mean in SQL?

VarBinary is a variable width data type. The syntax for declaring Binary variable is varbinary(n) , where n defines the maximum size in bytes. The varbinary data type uses actual length of the data entered + 2 bytes as the storage.

How do I get the size of a VARBINARY in SQL?

The VARBINARY(MAX) field allocates variable length data up to just under 2GB in size. You can use DATALENGTH() function to determine the length of the column content.

Is VARBINARY same as blob?

BLOB and TEXT differ from VARBINARY and VARCHAR in the following ways: For indexes on BLOB and TEXT columns, you must specify an index prefix length. For CHAR and VARCHAR, a prefix length is optional.


2 Answers

I too have encountered a similar problem and after much experimentation I noticed that when using Nhibernate to generate my schema to a file the generated column type was always length 8000.

Setting setting CustomSqlType to Varbinary(max) as suggested above made no difference, however, this work around in my FluentMapping seemed to do the trick:

Map(x => x.LogoBytes).CustomType("BinaryBlob").Length(1048576).Nullable();   

The length of course is an arbitrary amount but I think it should be set to something less than int.Max. I am new to Nhibernate so I'm still figuring things out but I'd be interested to know if this helps you.

like image 112
sbeskur Avatar answered Oct 11 '22 06:10

sbeskur


In 3.0.0GA, the following mapping seems to do the trick:

        <property name="Data" type="Serializable" length="2147483647" /> 
like image 30
Michael Teper Avatar answered Oct 11 '22 05:10

Michael Teper