programming

Reading binary data (image fields) from SQL Server 2005

I'm working on a project which requires me to read (with C#) existing SQL Server 2005 'Image' datatype data, making it useful to the end user in an ASP.NET application. The trouble is, the mime type for the data stored in each row is not stored...there isn't an easy way to tell, in advance, if the data I'm pulling out is a pdf, gif, jpg, zip, docx, xml, bmp or some other file type.

Looking closely at the stored data, I noticed that they start with consistent values (headers), and there are only a handful of different headers there.

Examples:
0x1F8B08....
0X255044....

C# Brain dump

In a moment of severe mental weakness, I couldn't remember how to assign an integer's value as a text (string) value (yes, really). I found it funny enough to write up here. Here's what I tried (this actually works!):

this.lblItemID.Text = ((int)0).ToString();

After I wrote it, I just stared at it for a few seconds, before realizing the better (and easier, and obvious) way:

this.lblItemID.Text = "0';

I don't even know where that first sample came from...I was just typing along and suddenly, there it was. Sheesh.

Syndicate content