1

I have a c# function that needs to write to a SQL image column. I have the image in a byte array. The standard seems to be the use of SqlCommand while passing the byte array as a parameter of type System.Data.SqlDbType.Image.

Unfotunately, my function can only use text queries (don't ask why) so I have to find a way to use T-SQL commands only. What I have so far can write to the column but I don't know what format of string to make the image blob string.

sql = "DECLARE @ptrval binary(16)" +
"SELECT @ptrval = textptr(Photo) FROM EMPhoto WHERE Employee='" + employeeID + "'" +
"WRITETEXT EMPhoto.Photo @ptrval " + imageByteArrayAsString;

I've tried converting imageByteArray to a Hex string and Binary string but it doesn't seem to end up correct in SQL or in the application that reads it.

2
  • Hi there. There's nothing unfortunate about that. Databases aren't meant to store images. Just because you can doesn't mean you should. Can you tell me what business requirement makes you want to sidestep local storage? Commented Apr 4, 2012 at 8:52
  • Wimbo, I agree. Local storage would be my preference as well. The function I'm making is only middleware for a third party application. I don't have control over the application that uses sql image datatypes. Commented Apr 4, 2012 at 20:07

1 Answer 1

1

A T-SQL Binary constant is an unquoted hexidecimal string prefixed with 0x. ie 0xFFD8FFE0...

string imageByteArrayAsString = "0x" + BitConverter.ToString(image).Replace("-", string.Empty);
Sign up to request clarification or add additional context in comments.

1 Comment

Ha! 0x with no quotes. ie 0xFFD8FFE0... Thanks Adiran.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.