将 VARBINARY(MAX) 从 SQL Server 读取到 C#
我需要从 SQL Server 2008 读取数据行。其中一列的类型是 VARBINARY(MAX)
。在 C# 中,我想使用 out 参数来读取它(并且给定的场景大部分满足需求)。
但我需要指定参数变量大小来填充 C# 变量。 这里我假设 8000 就足够了...但是谁知道呢:
database.AddOutParameter(command, "vbCertificate", DbType.Binary, 8000);
所以问题是:
- SQL Server 2008 的 MAX 数量是多少?
- 在这种情况下使用 out 参数可以吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
VARBINARY(MAX)
的最大大小为 2 GB 数据 - 2'147'483'648 字节。就您而言,在 C# 中定义它时,我建议使用
int.MaxValue
作为要提供的值。是的,如果您想从 SQL Server 2008 表中获取字节数组,那么您绝对可以使用
out
参数。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.正如@marc_s 所说,我只想添加一些内容。
有两种数据类型
二进制 [ ( n ) ]
长度为 n 字节的固定长度二进制数据,其中 n 是 1 到 8,000 之间的值。存储大小为n字节。
varbinary [ ( n | max) ]
可变长度二进制数据。 n 可以是 1 到 8,000 之间的值。 max表示最大存储大小为2^31-1(等于int.MaxValue即2,147,483,647)字节。存储大小为输入数据的实际长度+2字节。输入的数据长度可以是 0 字节。
如果您指定 max 那么您应该关注 varbinary 而不是二进制
database.AddOutParameter(command, "vbCertificate", DbType.Binary, 8000);database.AddOutParameter(command, "vbCertificate", SqlDbType .VarBinary, int.MaxValue);
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);
我还没有在任何地方看到 MAX 的明确尺寸。您是否尝试过使用 DbType.Object 执行此操作? msdn 上将其定义为任何未明确定义的类型的通用类型。我会尝试一下,看看它是否有效。否则,我可能会坚持使用您选择的 DbType.Binary 。
I haven't seen an explicit size anywhere for MAX. Have you tried executing this using DbType.Object? That is defined on msdn as a general type for any type not explicitly defined. I would try that and see if it works or not. Otherwise, I would probably stick to DbType.Binary as you chose.