Community discussion forum

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

  • 9 months 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
    Post was edited on 20/02/2009 14:43:00 Report abuse
  • 9 months 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
  • 9 months 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).

Want to stay in touch with what's going on? Follow us on twitter!