Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read VARBINARY(MAX) from SQL Server to C#

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:

  1. What is the size of MAX in number for SQL Server 2008?
  2. Is this ok to use out parameter for this scenario?
like image 500
Sasha Reminnyi Avatar asked Feb 09 '11 17:02

Sasha Reminnyi


People also ask

How do you read VARBINARY data?

INSERT INTO #TempTable(PK, VarBinaryColumn) SELECT PK, VarBinaryColumn FROM dbo. YourPermanentTable; If you need to convert the varbinary data back to the original file text format in T-SQL, you can use CAST or CONVERT to convert to varchar or nvarchar as long as the data was originally ASCII or Unicode.

What is VARBINARY Max in SQL Server?

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. The data that is entered can be 0 bytes in length. The ANSI SQL synonym for varbinary is binary varying.

What is VARBINARY C#?

VARBINARY is an SQL data type, nothing to do with C#.

What does VARBINARY mean in SQL?

SQL VarBinaryMax. The VARBINARY data type holds variable-length binary data. Use this type when the data is expected to vary in size. The maximum size for VARBINARY is 8,000 bytes. As an aside, the word VARBINARY stands for varying binary.


1 Answers

The max size for VARBINARY(MAX) is 2 GB of data - 2'147'483'648 bytes.

In your case, when defining it in C#, I would recommend using int.MaxValue as the value to provide.

And yes, if you want to get the array of bytes back from the SQL Server 2008 table, then you can most definitely use an out parameter.

like image 123
marc_s Avatar answered Sep 21 '22 04:09

marc_s