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

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 7
Reputation: aa361 is an unknown quantity at this point 
Solved Threads: 0
aa361 aa361 is offline Offline
Newbie Poster

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

 
0
  #1
Feb 20th, 2009
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??

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: aa361 is an unknown quantity at this point 
Solved Threads: 0
aa361 aa361 is offline Offline
Newbie Poster

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

 
0
  #2
Feb 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 45
Reputation: bcasp is an unknown quantity at this point 
Solved Threads: 10
bcasp bcasp is offline Offline
Light Poster

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

 
0
  #3
Feb 20th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: aa361 is an unknown quantity at this point 
Solved Threads: 0
aa361 aa361 is offline Offline
Newbie Poster

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

 
0
  #4
Feb 20th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 16
Reputation: danielernesto is an unknown quantity at this point 
Solved Threads: 1
danielernesto danielernesto is offline Offline
Newbie Poster

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

 
0
  #5
Feb 20th, 2009
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??
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

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

 
0
  #6
Feb 20th, 2009
As danielernesto said, store it to a column type that supports byte arrays such as Image or VarBinary(max).

// Jerry
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: aa361 is an unknown quantity at this point 
Solved Threads: 0
aa361 aa361 is offline Offline
Newbie Poster

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

 
0
  #7
Feb 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 436
Reputation: JerryShaw is on a distinguished road 
Solved Threads: 72
JerryShaw JerryShaw is offline Offline
Posting Pro in Training

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

 
0
  #8
Feb 21st, 2009
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.

  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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: aa361 is an unknown quantity at this point 
Solved Threads: 0
aa361 aa361 is offline Offline
Newbie Poster

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

 
0
  #9
Feb 21st, 2009
It worked!
Thank you so much for your time. Everybody.
And specially Jerry, you are just awsome.

Much appreciated.

Ash
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC