Storing bytearray in VarBinary column in SQL Server only inserts one byte

I have the following problem, I am trying to save byte[]

to a database and I just realized that it only works for one byte.

I have a number of floats that I am converting to bytes [] and use that as a parameter:

param = new SqlParameter(name, type, ((byte[])value).Length);

      

type VarBinary

, value is an array of bytes.

I add this parameter to mine SqlCommand

and before it is executed, the entire byte array "sits" in this parameter, and _msize

this parameter is correct (20 for 20 bytes I assume is correct). My SQL Server only shows me 1 byte and also tries to return it. I only get one byte. My column VarBinary(200)

.

Any suggestions?

+3


source to share


1 answer


If you are using a stored procedure and you have defined your parameter as simple varbinary

- you will get the default length of 1 byte according to the MSDN documentation :

When n is not specified in a declaration definition or variable declaration, the default length is 1 . If n is not specified by the CAST function, the default length is 30.

So, if you have a stored procedure with

@MyData VARBINARY

      



then you only have one byte - you need to change that to something like

@MyData VARBINARY(200) 

      

or something else that suits you

+4


source







All Articles