What variable type should I use to save an image?

In your .NET code, you'll probably want to use System.Drawing.Image or a derived class. Ultimately, you'll have to stream those bytes out to SQL Server one way or another, but you don't need to use a byte array from the beginning. All picture related types in .NET offer some kind of streaming support.

On the SQL Server side, by all means, use a VARBINARY(MAX) type - it's binary, it's up to 2 GB in size, it's fast, it's perfect for that use case. Up to an average image size of about 1 MB, this is probably your best bet - even better than using the SQL Server 2008 FILESTREAM attribute (which is great if you have lots of really really large images, larger than 1 MB on a regular basis; here, the binary file is itself stored in the server machine's file system under database control).


System.Drawing.Image or System.Drawing.Bitmap


You have it correct - use a byte array in your class. For images less than 1 MB, use a varbinary(max) column in SQL server. If the images are over 1 MB and you're using SQL Server 2008, consider using a FILESTREAM column.

You can use the chart on this MSDN page to refer to C#/Sql data type mappings.

Tags:

C#

Sql

Types