Solved

Transferring Image DataType between Two Databases

Posted on 2006-10-30
6
243 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:AMLabels
  • 3
  • 3
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 17832705
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.
0
 

Author Comment

by:AMLabels
ID: 17832812
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.
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17832844
This is code fragment from the function which reads image from database:

        string strCmd = String.Format("SELECT photo FROM Photos WHERE id = {0}",
                                      item.Id);
        SqlCommand cmd = new SqlCommand(strCmd, sqlConn);
                       
        // Set the description
        lblDesc.Text = item.Desc;

        // Get bytes return from stored proc
        byte[] b = (byte[])cmd.ExecuteScalar();

Now you can call function InsertImage which requires byte[] parameter. Pass b to it, and image is copied from one database to another.
0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 

Author Comment

by:AMLabels
ID: 17833874
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?
0
 

Author Comment

by:AMLabels
ID: 17833959
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.
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 17833973
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.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Not showing JavaScript in the list 5 53
Angular - break the dataset in chunks? 7 50
Keyboard 2 33
Where did System.Data.Objects go? 2 27
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question