将字节数组保存到SQL Server中的VarBinary列中只能插入一个字节 [英] saving bytearray to VarBinary column in SQL Server inserts only one byte
问题描述
我遇到了以下问题-我试图将 byte []
保存到数据库中,但我才发现它只能用于一个字节。
I have a following problem - I'm trying to save byte[]
to database and I just figured out that it works for one byte only.
我有一些浮点数,我将其转换为byte []并将其用作参数:
I have number of floats that I convert to byte[] and use that as a parameter:
param = new SqlParameter(name, type, ((byte[])value).Length);
类型是 VarBinary
,值是字节
我将该参数添加到我的 SqlCommand
中,并且在执行该命令之前,整个字节数组都坐了在该参数中,该参数的 _msize
是正确的(我认为20表示20字节是正确的)。我的SQL Server显示仅保存了1个字节,还尝试将其取回,但我只得到一个字节。我的专栏是 VarBinary(200)
。
I add that parameter to my SqlCommand
and just before it gets executed the whole byte array "sits" in that parameter and _msize
of that parameter is correct (20 for 20 bytes I assume is correct). My SQL Server shows me only 1 byte saved, also trying to retrieve it back I'm getting only one byte. My column is VarBinary(200)
.
有任何建议吗?
推荐答案
如果您使用存储过程,并且已将参数定义为 varbinary
-您将根据 MSDN文档获取默认长度 1个字节 :
If you're using a stored procedure, and you've defined your parameter as just varbinary
- you'll get a default length of 1 byte as per MSDN documentation:
当在数据定义或变量声明语句中未
指定时,默认长度为1 。如果CAST函数未指定n,则默认长度为30。
When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.
因此,如果您有一个存储过程,
So if you have a stored procedure with
@MyData VARBINARY
然后您只有一个字节-您需要将其更改为类似的内容
then you have just one single byte - you need to change that to something like
@MyData VARBINARY(200)
或其他适合您的东西
这篇关于将字节数组保存到SQL Server中的VarBinary列中只能插入一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!