Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

sql

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?

like image 357
DaveL Avatar asked Aug 31 '25 01:08

DaveL


2 Answers

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

C# ByteArray to string conversion and back

like image 143
Jesse Petronio Avatar answered Sep 02 '25 14:09

Jesse Petronio


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

    ...
like image 39
Chris Catignani Avatar answered Sep 02 '25 15:09

Chris Catignani