Read VARBINARY(MAX) from SQL Server to C#
ado.net, c#, sql, sql-server
Solution
As @marc_s said, I will just want to add something.
There are two data types
binary [ ( n ) ]
Fixed-length binary data with a length of n bytes, where n is a value from 1 through 8,000. The storage size is n bytes.
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 (equals int.MaxValue i.e. 2,147,483,647) bytes. The storage size is the actual length of the data entered + 2 bytes. The data that is entered can be 0 bytes in length.
if you are specifying max then your concern should be with varbinary instead of binary
database.AddOutParameter(command, "vbCertificate", DbType.Binary, 8000);
database.AddOutParameter(command, "vbCertificate", SqlDbType.VarBinary, int.MaxValue);
Problem
I need to read data row from SQL Server 2008. The type of one of the columns is `VARBINARY(MAX)`. In C# I want to use out parameter to read it (and given scenario satisfies the needs mostly). But I need to specify the parameter variable size to fill the C# variable. Here I assume that 8000 is enough... But who knows: ``` database.AddOutParameter(command, "vbCertificate", DbType.Binary, 8000); ``` So the questions are: - What is the size of MAX in number for SQL Server 2008? - Is this ok to use out parameter for this scenario?