0

Just to be clear, I'm NOT trying to insert into SQL Server directly from C#.

I'd like to convert a C# byte array into a binary string so that I can paste it into a SQL Script like the following:

insert into Files (FileId, FileData) values (1, 0x010203040506)

How can I do this?

adam0101
  • 29,096
  • 21
  • 96
  • 174

1 Answers1

2

Binary constants in SQL Server 'have the prefix 0x and are a string of hexadecimal numbers'.

You can convert each byte to hexadecimal using the "x2" format string and combine the result with String.Join():

var bytes = new byte[] {1, 2, 3, 4, 5, 6};
var binaryString = "0x" + string.Join("", bytes.Select(b => b.ToString("x2")));
Phil Ross
  • 25,590
  • 9
  • 67
  • 77