Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert binary data into SQL from c# without a stored procedure

Does anyone know whether if its possible to insert binary data into an SQL field from c# without using a stored procedure?

For example - convert a byte array into base64 or something like that and then use a text command like the following...

String.Format("update A set B = {0} where C = D", Convert.ToBase64String(b));

where b is a byte array.

Thanks

like image 381
Grant Avatar asked Dec 23 '09 23:12

Grant


1 Answers

Of course, you should use parameterized statements, but if you really need to, you can do it like this:

byte[] b = null; //(your input should be a byte[])
String.Format("update A set B = 0x{0} where C = D", BitConverter.ToString(b).Replace("-", "").ToLower());

SQL Server expects binary data in a hexadecimal format without hyphens and in lower case with a prefix of '0x'

like image 81
erikkallen Avatar answered Sep 24 '22 20:09

erikkallen