0

I'm trying to update a varbinary in my MSSQL with a binary array of an image. I tried using this query, which doesn't seem to work:

string sql = "UPDATE table SET bdata='" + imagedata + "' WHERE ID=1";

This is just an example of a query I used. Why doesn't it work? And I prefer not using SqlCommand.

bdata is of type varbinary(max) and imagedata is a byte array.

What am I doing wrong that it's not working? An exception tells me I need to use CONVERT or something like that..

This is the given exception when trying to run it:

Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.

8
  • What do you mean by doesn't seem to work? You get any exception or error message? You need to use + just before WHERE ID=1 as well. Also you should use it in a string like + WHERE ID=1 (WHERE ID=1 part should beetween " since it is a string) Commented May 18, 2015 at 8:47
  • What is the type of imagedata? If you "prefer not using SqlCommand" - then how do you executing your query? Add more details. Commented May 18, 2015 at 8:49
  • Added variable types. Commented May 18, 2015 at 8:54
  • I do use SqlCommand but in another class that I trigger by sending a string of the query I want. Commented May 18, 2015 at 8:55
  • Use parameters! Then you can just set the parameter value to a byte[] rather than messing with strings like you are here, that's also open to injection! Commented May 18, 2015 at 8:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.