| | |
How to store a byte array in a database and retrive it back as byte array?
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Feb 2009
Posts: 7
Reputation:
Solved Threads: 0
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??
Any help is greatly appreciated.
Ash
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)
SqlCommand cmd1 = new SqlCommand("INSERT INTO SIMILAR (ImageHash, ID) VALUES ((@hash), 26)", con); byte[] hash1 = GetHash(path1, 1); byte[] hash2 = GetHash(path2, 1); cmd1.Parameters.AddWithValue("@hash", hash1);
Any help is greatly appreciated.
Ash
•
•
Join Date: Feb 2009
Posts: 7
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Feb 2009
Posts: 7
Reputation:
Solved Threads: 0
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?
Thank you for all your help
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
•
•
Join Date: Nov 2006
Posts: 436
Reputation:
Solved Threads: 72
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.
// Jerry
C# Syntax (Toggle Plain Text)
SqlCommand cmd = new SqlCommand("Select ImageHash from SIMILAR WHERE ID=26", con); con.Open(); rdr = cmd.ExecuteReader(); rdr.Read(); byte[] temp = (byte[])rdr["ImageHash"];
// Jerry
![]() |
Other Threads in the C# Forum
- Previous Thread: DataGridView Control - Retrieving Information
- Next Thread: A problem in GDI
Views: 2004 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gdi+ http httpwebrequest image index input install java label list listbox login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml





