How to save a byte array (byte[]) in a database and retrive it back as byte array?

csharp , byte , byte array , system.byte , database , save , retrieve London, United Kingdom
  • 12 years ago
    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?? 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); The reason why I need the data back as byte[] is that one of the DLL function takes two byte arrays and compare them for similarity. Any help is greatly appreciated. Ash
  • 12 years ago
    Hi, The data type of the field has to image (or another type of blob) and then you can send it to the command as a parameter like you did. For retriving you have to cast the resault to a byte array. For example: DataTable dt = dal.GetData(); byte[] b = (byte[])dt.rows[0]["imageField"]; David
  • 12 years ago
    Hi David, Thank you very much. That solved the problem. I was able to save it but was having problems retrieving it. But now its solved. Thank you once again. :)

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are, in short, a perfect match” - Bill Bryson