Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

INSERT BYTE[] TO Sql Without parameter

Tags:

c#

sql

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!!!

like image 631
user1704457 Avatar asked Feb 18 '23 08:02

user1704457


1 Answers

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 + ")";
like image 175
Guffa Avatar answered Feb 28 '23 00:02

Guffa