Solved

Transferring Image DataType between Two Databases

Posted on 2006-10-30
6
235 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now