943,724 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 9214
  • C# RSS
Feb 20th, 2009
0

How to store a byte array in a database and retrive it back as byte array?

Expand Post »
Hello,

I am working on a project which aims to compare an image to from a database of images and find out similar looking images.

I am working with a DLL file which returns unique hash for each image.

The hash is an array of 768 bytes. (byte[])

My problem is how can I store this byte[] in database and later retrive it back as a byte[]?

I have tried many methods like storing it in a varbinary and varchar format but when I look in the database, its just a long number (might be string) and there is no information about which element goes where in array if that makes sense??

C# Syntax (Toggle Plain Text)
  1. SqlCommand cmd1 = new SqlCommand("INSERT INTO SIMILAR (ImageHash, ID) VALUES ((@hash), 26)", con);
  2.  
  3. byte[] hash1 = GetHash(path1, 1);
  4. byte[] hash2 = GetHash(path2, 1);
  5.  
  6. cmd1.Parameters.AddWithValue("@hash", hash1);

Any help is greatly appreciated.

Ash
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aa361 is offline Offline
7 posts
since Feb 2009
Feb 20th, 2009
0

Re: How to store a byte array in a database and retrive it back as byte array?

Oh and the reason why I need the values back as byte array is because the DLL has another function which takes two byte arrays to compare them.

Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aa361 is offline Offline
7 posts
since Feb 2009
Feb 20th, 2009
0

Re: How to store a byte array in a database and retrive it back as byte array?

An easy approach would be to separate the values in the array by a comma before putting it into the database as a comma delimited string. Then when you bring it back from the database, just do a split on the comma. I'm sure there's better/more elegant solutions, but that seems like it would work.
Reputation Points: 23
Solved Threads: 10
Light Poster
bcasp is offline Offline
45 posts
since Apr 2008
Feb 20th, 2009
0

Re: How to store a byte array in a database and retrive it back as byte array?

Hi bcasp,
Thank you very much for your quick reply.

I did thought of saving the array as Comma Seperated values and checking for commas when retrieveing, but I am sure there are more neater ways of doing this. I mean it just doesn't feel like the best way of doing this?

Is there any way I can save values in the memory location in database and retrieve as it is? Like saving whatever is in the memory, save it in database?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aa361 is offline Offline
7 posts
since Feb 2009
Feb 20th, 2009
0

Re: How to store a byte array in a database and retrive it back as byte array?

Hello,

Why don't you try to make that byte array to image then save it, and retrive it like and image then convert the image to a byte array.

What do you think??
Reputation Points: 9
Solved Threads: 2
Newbie Poster
danielernesto is offline Offline
16 posts
since Nov 2007
Feb 20th, 2009
0

Re: How to store a byte array in a database and retrive it back as byte array?

As danielernesto said, store it to a column type that supports byte arrays such as Image or VarBinary(max).

// Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Feb 21st, 2009
0

Re: How to store a byte array in a database and retrive it back as byte array?

Saving the byte[] as VarBinary(MAX) is ok. But, the problem comes when retrieving it as byte array.

I will be working with hundreds of pictures so I can't store each and every single hash as image or external file.

Is there a method to read the VarBinary values from a database as byte[]? Could you please give an example code?

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=c:\\Test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"); 

SqlDataReader rdr = null;

SqlCommand cmd = new SqlCommand("Select ImageHash from SIMILAR WHERE ID=26", con); 

            try
            { 
                con.Open(); 
                rdr = cmd.ExecuteReader(); 
                rdr.Read(); 

                //      And there is something here?? 
                //      While reader is reading, get the values as Bytes? 

                //      byte[] temp = rdr.GetBytes(); (I couldn't figure out what should I put in the arguments)
                                                      "No overload for method 'GetBytes' takes '0' arguments"

            }

Thank you for all your help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aa361 is offline Offline
7 posts
since Feb 2009
Feb 21st, 2009
0

Re: How to store a byte array in a database and retrive it back as byte array?

You do not need to use the GetBytes. It is available if you need to do special things with that byte array, however you want the entire array as you originally placed it into the database.

C# Syntax (Toggle Plain Text)
  1. SqlCommand cmd = new SqlCommand("Select ImageHash from SIMILAR WHERE ID=26", con);
  2. con.Open();
  3. rdr = cmd.ExecuteReader();
  4. rdr.Read();
  5. byte[] temp = (byte[])rdr["ImageHash"];

// Jerry
Reputation Points: 69
Solved Threads: 75
Posting Pro in Training
JerryShaw is offline Offline
465 posts
since Nov 2006
Feb 21st, 2009
0

Re: How to store a byte array in a database and retrive it back as byte array?

It worked!
Thank you so much for your time. Everybody.
And specially Jerry, you are just awsome.

Much appreciated.

Ash
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aa361 is offline Offline
7 posts
since Feb 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: DataGridView Control - Retrieving Information
Next Thread in C# Forum Timeline: A problem in GDI





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC