Inserting and Retrieving An Image In SQL Server 2005
If you’re looking for a solution to store and retrieve a picture from a table in a SQL Server 2005 database, I hope that these two functions (StoreImageInDatabase at line 4 and RetrieveImageFromDatabase at line 72) will satisfy your requirement. I had to implement such functionality in a project and decided to keep it here for further reference. It’s heavily commented for those developers that are taking their first steps with ADO.NET. Feel free to share your own implementation too. There are so many ways to do this.
////// Stores an image to the database /// private static void StoreImageInDatabase( string imageFilename, ImageFormat imageFormat ) { // Make sure the image file exists prior to proceeding with the storage. // Note: The File.Exists method will return false whether the imageFilename parameter is null or empty. if ( !File.Exists( imageFilename ) ) throw new FileNotFoundException( “The image file was not found”, imageFilename ); // Load the image from the filename received as a parameter. Image image = Image.FromFile( imageFilename ); // Holds the byte representation of the image. Byte[] content; // Use a MemoryStream to store the image representation as stream of bytes. using ( var stream = new MemoryStream() ) { image.Save( stream, imageFormat ); // Convert the stream into an array of bytes because that’s the value we’ll pass to our SQL parameter. content = stream.ToArray(); } // Our connection string (put it in your configuration file if needed) const string connectionString = “INSERT YOUR DATABASE’S CONNECTION STRING HERE”; // Create an SQL connection based on a connection string pre-defined (the using statement will close/dispose the connection on our behalf even if an exception should occur). using ( var connection = new SqlConnection( connectionString ) ) { // The SQL statement/stored procedure used to store our image. // Just make sure that you have a parameter named ‘@PICTURE’ of type Image as well as a column named ‘PICTURE’ of type Image too in the table. const string sqlStatement = “UPDATE SOME_TABLE SET PICTURE = @PICTURE WHERE SOME_CONDITION”; // Create an SQL command based on our stored procedure and SQL connection. using ( var command = new SqlCommand( sqlStatement, connection ) ) { // Explicitly state that we’ll be using a SQL statement or a stored procedure (CommandType.StoredProcedure). command.CommandType = CommandType.Text; // Define the SQL parameter which will hold our image to store. var parameter = new SqlParameter( “@PICTURE”, SqlDbType.Image ) { Value = content }; command.Parameters.Add( parameter ); try { // Open the connection. connection.Open(); // Execute the stored procedure to store our image in the database. command.ExecuteNonQuery(); MessageBox.Show( “Image stored successfully”, “Image storage”, MessageBoxButtons.OK, MessageBoxIcon.Information ); } catch ( SqlException ex ) { MessageBox.Show( ex.Message, “SQLException”, MessageBoxButtons.OK, MessageBoxIcon.Error ); // Other error handling, logging, etc. } } } } ////// Retrieves an image from the database /// private static Image RetrieveImageFromDatabase() { // The image that will be retrieved Image image = null; // Our connection string (put it in your configuration file if needed) const string connectionString = “INSERT YOUR DATABASE’S CONNECTION STRING HERE”; // Create an SQL connection based on a connection string pre-defined (the using statement will close/dispose the connection on our behalf even if an exception should occur). using ( var connection = new SqlConnection( connectionString ) ) { // The SQL statement/stored procedure used to retrieve our image. // Just make sure that you have a column named ‘PICTURE’ of type Image. const string sqlStatement = “SELECT PICTURE FROM SOME_TABLE WHERE SOME_CONDITION”; // Create an SQL command based on our stored procedure and SQL connection. using ( var command = new SqlCommand( sqlStatement, connection ) ) { try { // Explicitly state that we’ll be using a SQL statement or a stored procedure (CommandType.StoredProcedure). command.CommandType = CommandType.Text; // Open the connection. connection.Open(); // Execute the query and build a DataReader with the results. var reader = command.ExecuteReader(); Debug.Assert( reader != null ); // Since we only have one image stored in the table (this is an example after all…), we’ll only have one image to retrieve from our table. // If you have to retrieve one specific image (or more) from a collection of images in the table, then you can precise which one to retrieve via a parameter. reader.Read(); // Since the only column of our table is used to store/retrieve an image, its column index is zero. // We simply cast that column into an array of bytes. var content = (byte[])reader[0]; // Create a stream based on the entire content of our buffer. using ( Stream stream = new MemoryStream( content, 0, content.Length ) ) { // Write the content (array of bytes) to our stream. stream.Write( content, 0, content.Length ); // Create an image object based on the stream. image = Image.FromStream( stream ); } } catch ( SqlException ex ) { MessageBox.Show( ex.Message, “SQLException”, MessageBoxButtons.OK, MessageBoxIcon.Error ); // Other error handling, logging, etc. } } } return image; }

A former co-worker of mine,
According to the MSDN documentation concerning the
Right-click anywhere on the toolbar container and click on the Source Control – Team Foundation item (the list is in alphabetical order). Peek on the huge screenshot on the right for guidance.