DaveL
DaveL

Reputation: 87

How to retrieve a byte array stored as Varbinary(max)

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?

Upvotes: 4

Views: 25438

Answers (2)

Chris Catignani
Chris Catignani

Reputation: 5306

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"]);

    ...

Upvotes: 1

Jesse Petronio
Jesse Petronio

Reputation: 723

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

C# ByteArray to string conversion and back

Upvotes: 4

Related Questions