What SqlDbType maps to varBinary(max)? SqlDbType.VarBinary says that it is limited to 8K. SQL Server documentation says that varbinary(max) can store aprrox. 2GB. But SqlDbType.VarBinary says that it is limited to 8K.
Specifies SQL Server-specific data type of a field, property, for use in a System. Data.
SqlDbType = dataType ; Specifies the SQL Server data type of the parameter, using the SqlDbType enumeration in the System. Data namespace. If this property is set, the value of the Parameter is converted to this type before it's passed to the data source. The default is a SqlDbType.
varbinary [ ( n | max ) ] 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.
SqlDbType.VarBinary with length -1 is the equivalent of VARBINARY(MAX), at least in theory. But the problem is a bit more complex, as there is also a type (not an enum value), namely SqlTypes.SqlBytes
which can be used. And there is SqlTypes.SqlFileStream
which can be used also for VARBINARY(MAX)
types, when they have the FILESTREAM
attribute.
But the problem is that none of these enums or types cover the real issue with working with VARBINARY(MAX)
columns in ADO.Net: memory consumption. All these types, when used 'out-of-the-box', will create copies of the value allocated as a single array in memory, which is at best unperformant, but as content gets larger becomes down right impossible to use because allocation failures. I have a couple of articles that show the proper way to handle VARBINARY(MAX)
values in ADO.Net using streaming semantics that avoid the creation of in-memory copies of the entire content:
Try this:
SqlParameter blobParam = new SqlParameter("@blob", SqlDbType.VarBinary, buffer.Length);
blobParam.Value = buffer;
cmd.Parameters.Add(blobParam);
See if that works
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With