Link to home
Start Free TrialLog in
Avatar of AMLabels
AMLabels

asked on

Transferring Image DataType between Two Databases

I am writing a comms application that transfers data from a main PC application to a mobile device (using SQL Mobile) and the only datatype I'm having trouble transferring is Image.

I'm using the ExecuteReader method of the SqlCommand object, and then using GetValues() of the DataReader to populate an array with the database values.

i.e.

reader.GetValues(argArray);

The argArray now contains the value of an image field from the main PC app, but I am unsure of how to convert this value back to a suitable format to use in a SQL Statement for the mobile database.

Essentially, I'm looking for a way to convert a value (of type Object) into a format that conforms to the SQL Server IMAGE datatype.

Can anyone help me with this?
Avatar of AlexFM
AlexFM

http://www.codeproject.com/cs/database/albumviewer.asp

This sample shows how to convert image file to SQL database field, and how to read image from database and save to file. You can remove file handling and use read and write code fragments for your task.
Avatar of AMLabels

ASKER

Without using the FileStreams, how can I convert the Object instance to a byte[]?

This code raises an InvalidCastException on the second line:

Object obj = argsArray[index];
byte[] img = (byte[])obj;

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This works, except that the SQL parameter for images must be formatted as Hex, not base 2.

Does the .NET Framework provide the functionality to convert a byte array into a Hex string?
Whoops, made a mistake there. I meant to say the format of the image in SQL Statements needs to be Hex and not Base 10.
How does CodeProject sample work without this conversion?
There is poorly documented XmlConvert.ToBinHexString method which possibly does this. I understand that such conversion is required for XML files, but why does SQL need this? Please check again, I think this should work directly.