0

I'm pretty new to C# (As I'm sure you'll be able to tell) and I know this question has been asked a few times but none of the solutions have worked for me.

I'm trying to pull a Varbinary(MAX) value out of a database and store it as a string. I'm using a SqlDataReader for lifting some of the other columns out which works fine. All I'm using for them is .ToString(), but if I use ToString() for the Logo it returns System.Byte[]

    public TemplateData(SqlDataReader dr)
    {
        initialiseData();
        if (dr.HasRows)
        {

            Logo = dr["Logo"].ToString();
            TemplateId = dr["TemplateId"].ToString();
            Comment = dr["Comment"].ToString();
            SchemeCode = dr["SchemeCode"].ToString();
            Version = dr["Version"].ToString();
        }
    }

There is a plethora of issues I'm having aswell but I'll leave them for now.

Cheers!

Bigtingz92
  • 129
  • 1
  • 15

1 Answers1

1

Cast as a byte array then convert, for example:

byte[] data = (byte[])row["BinaryColumn"];
string str = Encoding.UTF8.GetString(data);

However this is making the big assumption the binary is some kind of string, otherwise you might want Base-64:

string str = Convert.ToBase64String(data);
Lloyd
  • 29,197
  • 4
  • 84
  • 98