C # / asp.net show binary data from database

I was looking for how to extract binary data from a database. My query works fine as it shows binary data in SQL manager.

But in Visual Studio 2010, it just shows me <Binary Data>which, as I personally think, is not a problem, but if I try to restore it and save it in a variable, it is simply empty. Any other query works as long as it has nothing to do with binary data ...

And I do not want to use datagrid or anything else, since I only need this as a variable.

SqlCommand CMDbinary = new SqlCommand("SELECT Binary_field AS binary "
                                      + "FROM BIN_table", abc_conn);

SqlDataReader Bin_retriever = CMDbinary.ExecuteReader();
while (Bin_retriever.Read())
{
    BIN.Add(Bin_retriever["binary"].ToString());
}
abc_conn.Close();

I know that this is not complete code, but everything goes wrong here, I suppose I need to do something special to display binary information, since (I will say it again) it works if I change Binary_fieldto UID(which is varcharin the database).

Any help on this is really appreciated, thanks in advance!

+2
source share
1 answer
 Bin_retriever["binary"].ToString()

looks deadly. For binary data you can get here byte[]. You cannot just call ToString()on such - you are likely to get a type name. So how would you like to display it? hexagon?

byte[] raw = (byte[]) Bin_retriever["binary"];

Now format as you select. For instance:

var builder = new StringBuilder(raw.Length * 2);
for(int i = 0 ; i < raw.Length ; i++)
    builder.Append(raw[i].ToString("x2"));
string hex = builder.ToString();
+3
source

All Articles