I want to make a raw SQL query that adds an encrypted value (byte[]
) to an SQL column (varbinary
) without using parameter like this:
byte[] value = GetEncryptedValue();
string query = "INSERT INTO Table1 VALUES('" + value.ToString() + "')";
the column datatype that I want insert into is varbinary
. I need some function that uses value.ToString() instead. How to write this ?
i just need function like master.dbo.fn_varbintohexstr in sql!!!
A binary literal takes the form 0x6ABCDEF
, i.e. 0x
followed by bytes in hexadecimal form.
You can use this to turn the byte array into a literal value:
string literal = "0x" + String.Join("", value.Select(n => n.ToString("X2")));
There are not apostrophes around the binary literal:
string query = "INSERT INTO Table1 VALUES(" + literal + ")";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With