?
Solved

Transferring Image DataType between Two Databases

Posted on 2006-10-30
6
Medium Priority
?
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

764 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