How to manually generate a SQL statement (SQLCommand) containing binary data ?

T

TheSteph

How can I manually generate a SQL statement (SQLCommand) containing binary
data ?
I'd like to write all the text of the SQL statement for that operation...

Example :
I Have Binary data (that represent an image)

Byte[] TmpByte;
TmpByte = ....BINARY DATA OF AN IMAGE....

Now I want to generate the SQL statement
to insert the content of TmpByte into
MYTABLE ( MyIndex = VARCHAR(25) / MyData = VARBINARY (1000) )

Something like that :
INSERT MYTABLE (MyIndex, MyData) VALUES ('Index 1', ???? )

But I don't know how to put in place of the ????
I was expecting that someting like '01010101....101001' could work but it
didn't.

Thank for any help !

Steph.
 
L

Larry Lard

TheSteph said:
How can I manually generate a SQL statement (SQLCommand) containing binary
data ?

Short answer: use parameters.

Just say if you need the long version...
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

You have to use a SqlCommand, I use the code below to add a file to the DB

SqlCommand com = new SqlCommand();
com.CommandText = "SaveDocument";
com.CommandType = CommandType.StoredProcedure;
SqlParameter param = com.Parameters.Add("@data", SqlDbType.Image);
//Read the file into memory
FileStream file = new FileStream( physicalname, FileMode.Open);
byte[] buff = new byte [ file.Length];
file.Read(buff, 0, Convert.ToInt32(file.Length));
file.Close();
param.Value = buff;
 
M

Marc Gravell

For larger binary (where the byte[] becomes an overhead), see also my
self-answered post titled "Writing BLOBs (through C#)" from a few minutes
ago...

Marc
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,


Marc Gravell said:
For larger binary (where the byte[] becomes an overhead), see also my
self-answered post titled "Writing BLOBs (through C#)" from a few minutes
ago...

Nice piece of code, will keep it around :)

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top