Monday, March 26, 2012

Pushing IMAGE from SQL CE onto SQL Server 2000

Hello!

We are trying to use RDA to Push an image field onto SQL Server 2000 from a mobile device, but are having problems. Does anyone have any experience with this? If so, maybe some sample code would be very much appreciated. (We have been struggling with this for a while).

byte[] imagearray = null;

string imagestring = "";

try

{

imagearray = (byte[])SQLCEDr.GetValue(SQLCEDr.GetOrdinal("CapturedImage"));

imagestring = System.Text.Encoding.Default.GetString(imagearray, 0, (imagearray.Length - 1));

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

sParameters = "'" + imagestring + "'" ;

try

{

rda.SubmitSql("EXEC sp_HH_WorkOrder_SuppliesUsed " + sParameters,scon);

}

Thanks,

Andy

Here’s the problem:

imagestring = System.Text.Encoding.Default.GetString(imagearray, 0, (imagearray.Length - 1));

This line would be valid for an array which contains text in the default encoding. But it contains binary image data which would be treated as text, corrupted and lost.

To insert/retrieve binary data you have to use parameterized query. In fact, you should use parameterized queries for everything - would save you lot of troubles with localization (e.g. infamous 10/25/2006 vs. 25.10.2006) and formatting not to mention more secure and better performing code. If you don't know how to use parameters, please look it up on MSDN or search this forum as it's been answered before.

No comments:

Post a Comment