Writing BLOB Values to a Data Source
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

Writing BLOB Values to a Data Source 

You can write a binary large object (BLOB) to a database as either binary or character data, depending on the type of field at your data source. To write a BLOB value to your database, issue the appropriate INSERT or UPDATE statement and pass the BLOB value as an input parameter (see Working with Command Parameters). If your BLOB is stored as text, such as a SQL Server text field, you can pass the BLOB as a string parameter. If the BLOB is stored in binary format, such as a SQL Server image field, you can pass an array of type byte as a binary parameter.


A BLOB may be quite large and thus may consume significant system memory when written as a single value, decreasing application performance. To reduce the amount of memory used when writing a BLOB value, you can write the BLOB to the database in "chunks". The process of writing a BLOB to a database in this way depends on the capabilities of your data source. For an example of writing a BLOB value to SQL Server in chunks, see Conserving Resources When Writing BLOB Values to SQL Server.

The following code example adds employee information to the Employees table in the Northwind database. A photo of the employee is read from a file and added to the Photo field in the table, which is an image field.

public static void AddEmployee(
  string lastName, 
  string firstName, 
  string title, 
  DateTime hireDate, 
  int reportsTo, 
  string photoFilePath, 
  string connectionString)
  byte[] photo = GetPhoto(photoFilePath);

  using (SqlConnection connection = new SqlConnection(

  SqlCommand command = new SqlCommand(
    "INSERT INTO Employees (LastName, FirstName, " +
    "Title, HireDate, ReportsTo, Photo) " +
    "Values(@LastName, @FirstName, @Title, " +
    "@HireDate, @ReportsTo, @Photo)", connection); 

     SqlDbType.NVarChar, 20).Value = lastName;
      SqlDbType.NVarChar, 10).Value = firstName;
      SqlDbType.NVarChar, 30).Value = title;
       SqlDbType.DateTime).Value = hireDate;
      SqlDbType.Int).Value = reportsTo;

      SqlDbType.Image, photo.Length).Value = photo;


public static byte[] GetPhoto(string filePath)
  FileStream stream = new FileStream(
      filePath, FileMode.Open, FileAccess.Read);
  BinaryReader reader = new BinaryReader(stream);

  byte[] photo = reader.ReadBytes((int)stream.Length);


  return photo;

See Also

© 2016 Microsoft