4

I'm trying to store a byte array in the database (T-SQL) and currently I'm using varbinary(max). It successfully stores the data but I don't know how to convert it back to a byte array. Anyone knows how? Am I using the right datatype in the database?

StoreTestData(Encoding.ASCII.GetBytes("test123".ToCharArray()));

Results in 0x74657374313233

How to I get my result into a byte[] again?

3
  • I would have thought that retrieving a binary field from the database would give you a byte array - what are you getting? Also you haven't shown us any of your code for retrieving the code though so its hard to tell you what you are doing wrong. Commented Jun 11, 2014 at 8:58
  • actually, I dont retrieve it. That is was I asking on how to do.. I posted the result which was posted in the database. Commented Jun 11, 2014 at 9:00
  • So its not so much how to get your result into a byte array as it is how to get a result out of the database? Commented Jun 11, 2014 at 11:03

2 Answers 2

4

I believe you can find the answer to this question here:

C# ByteArray to string conversion and back

Sign up to request clarification or add additional context in comments.

3 Comments

Ty Jesse. I dont know why someone downrated your answer. I'm gonna accept this. Didnt realize I should read it as a string.
I didn't downvote but I assume it is because this doesn't talk about how to retrieve the data from the database and when it comes from the database if it is a varbinary(max) then I'd expect it to be retrieved as a byte array. Therefore this answer doesn't appear to answer the question. However, if it solved your problem then that's cool.
Link-only answers are generally frowned upon on Stack Overflow. In time it is possible for links to atrophy and become unavailable, meaning that your answer is useless to users in the future. It would be best if you could provide the general details of your answer in your actual post, citing your link as a reference.
1

Just cast the reader object back to a byte array.
In this case the database field "logo" is a varbinary(MAX)

    ... 

    SqlDataReader reader = cmd.ExecuteReader();

    byte[] tempLogo = (byte[])(reader["logo"]);

    ...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.