简体   繁体   English

从SQL Server读取VARBINARY(MAX)到C#

[英]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) . 我需要从SQL Server 2008读取数据行。其中一列的类型是VARBINARY(MAX) In C# I want to use out parameter to read it (and given scenario satisfies the needs mostly). 在C#中,我想使用out参数来读取它(并且给定的场景主要满足需求)。

But I need to specify the parameter variable size to fill the C# variable. 但我需要指定参数变量大小来填充C#变量。 Here I assume that 8000 is enough... But who knows: 在这里我假设8000就够了......但谁知道:

database.AddOutParameter(command, "vbCertificate", DbType.Binary, 8000);

So the questions are: 所以问题是:

  1. What is the size of MAX in number for SQL Server 2008? SQL Server 2008的MAX数量是多少?
  2. Is this ok to use out parameter for this scenario? 这种情况可以使用out参数吗?

The max size for VARBINARY(MAX) is 2 GB of data - 2'147'483'648 bytes. VARBINARY(MAX)的最大大小为2 GB数据 - 2'147'483'648字节。

In your case, when defining it in C#, I would recommend using int.MaxValue as the value to provide. 在您的情况下,当在C#中定义它时,我建议使用int.MaxValue作为要提供的值。

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. 是的,如果你想从SQL Server 2008表中获取字节数组,那么你绝对可以使用out参数。

As @marc_s said, I will just want to add something. 正如@marc_s所说,我只想添加一些东西。

There are two data types 有两种数据类型

binary [ ( n ) ] 二进制 [(n)]

Fixed-length binary data with a length of n bytes, where n is a value from 1 through 8,000. 固定长度的二进制数据,长度为n个字节,其中n是1到8,000的值。 The storage size is n bytes. 存储大小为n个字节。

varbinary [ ( n | max) ] varbinary [(n | max)]

Variable-length binary data. 可变长度的二进制数据。 n can be a value from 1 through 8,000. n可以是1到8,000之间的值。 max indicates that the maximum storage size is 2^31-1 (equals int.MaxValue ie 2,147,483,647) bytes. max表示最大存储大小为2 ^ 31-1(等于int.MaxValue,即2,147,483,647)个字节。 The storage size is the actual length of the data entered + 2 bytes. 存储大小是输入数据的实际长度+ 2个字节。 The data that is entered can be 0 bytes in length. 输入的数据长度可以是0个字节。

if you are specifying max then your concern should be with varbinary instead of binary 如果你指定max,那么你的关注点应该是varbinary而不是binary

database.AddOutParameter(command, "vbCertificate", DbType.Binary, 8000); database.AddOutParameter(command,“vbCertificate”,DbType.Binary,8000);

database.AddOutParameter(command, "vbCertificate", SqlDbType.VarBinary, int.MaxValue); database.AddOutParameter(command,“vbCertificate”,SqlDbType.VarBinary,int.MaxValue);

I haven't seen an explicit size anywhere for MAX. 我没有在MAX的任何地方看到明确的大小。 Have you tried executing this using DbType.Object? 您是否尝试使用DbType.Object执行此操作? That is defined on msdn as a general type for any type not explicitly defined. 这在msdn上定义为任何未明确定义的类型的通用类型。 I would try that and see if it works or not. 我会尝试,看看它是否有效。 Otherwise, I would probably stick to DbType.Binary as you chose. 否则,我可能会选择DbType.Binary。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM