Loading Image/BLOB from SQL Server

If you have an image column in your SQL table you can load the bytes using the following code.

int bufferSize = Convert.ToInt32(reader.GetBytes(7,0,null,0,int.MaxValue));
_ThumbnailImage = new Byte[bufferSize];
reader.GetBytes(7, 0, _ThumbnailImage, 0, bufferSize);


The first parameter is the column number, in this case 7, call GetBytes with a null buffer first to get the number of bytes needed.

Remember that an image column, holds bytes and therefore can contain any sort of document, not necessarily an image, could be a PDF


C# 3.5 – Code Snippets

In the intellisense list certain options will appear that state they are code snippets, e.g. foreach. To get the snippet to actually insert press TAB twice.

See MSDN for more details


Remember "null" is not the same as "DBNull"

When supplying null values to stored procedures, remember that you have to set the value to DBNull…

if (_UserId == null)
{
    cmd.Parameters.Add(new SqlParameter("@UserId",System.DBNull.Value ));
}
else
{
    cmd.Parameters.Add(new SqlParameter("@UserId", _UserId));
}


Next Entries