Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Insert a Byte[] Into an SQL Server VARBINARY Column

I have a byte array highlighted below, how do I insert it into a SQL Server database Varbinary column?

byte[] arraytoinsert = new byte[10]{0,1,2,3,4,5,6,7,8,9};  string sql =      string.format     (     "INSERT INTO mssqltable (varbinarycolumn) VALUES ({0});",WHATTODOHERE     ); 
like image 519
divinci Avatar asked Jun 30 '09 14:06

divinci


People also ask

How many bytes is a VARBINARY?

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 bytes.

Can you store a byte array in SQL?

In Sql Server, use image data type or varbinary data type, to store byte array data.

What datatype is VARBINARY?

Store raw-byte data, such as IP addresses, up to 65000 bytes. The BINARY and BINARY VARYING (VARBINARY) data types are collectively referred to as binary string types and the values of binary string types are referred to as binary strings.

What does VARBINARY mean in SQL?

VarBinary is a variable width data type. The syntax for declaring Binary variable is varbinary(n) , where n defines the maximum size in bytes. The varbinary data type uses actual length of the data entered + 2 bytes as the storage.


1 Answers

Try this:

"0x" + BitConverter.ToString(arraytoinsert).Replace("-", "") 

Although you should really be using a parameterised query rather than string concatenation of course...

like image 139
David M Avatar answered Oct 07 '22 18:10

David M