Introduction
You can define a BLOB as a large photo, document, audio, etc. saved in binary formats that you want to save in a database.
Saving and retrieving BLOBs in a database is more complex than querying string or numeric data.
The BLOB may be very large and if you try to move it in one piece will consume a lot of system memory and that for sure will affect your application performance.
To reduce the amount of system memory you have to break up the BLOB into smaller pieces.
There are a lot of classes that are designed for moving large amounts of binary data like BinaryRader, and BinaryWriter which exist in the System.IO namespace. In the next paragraphs, you will see how to use all of this.
Saving a BLOB value to the database
To save a BLOB value to the database we use FileStream and BinaryReader classes.
The next example will show you the process of saving a BLOB to a database.
string filePath = @"D:\My Movie.wmv";
// Open file stream for reading
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
// Read file bytes into byte array
byte[] blobValue = new byte[fs.Length];
fs.Read(blobValue, 0, (int)fs.Length);
fs.Close(); // Close the file stream
// Connect to database
SqlConnection connection = new SqlConnection("Data Source=.;Initial Catalog=BlobsDatabase;Integrated Security=SSPI");
// Prepare SQL command with parameters
SqlCommand command = new SqlCommand("INSERT INTO BlobsTable(BlobFileName, BlobFile) VALUES (@BlobFileName, @BlobFile)", connection);
SqlParameter fileNameParam = new SqlParameter("@BlobFileName", SqlDbType.NChar) { Value = Path.GetFileName(filePath) };
SqlParameter fileParam = new SqlParameter("@BlobFile", SqlDbType.VarBinary) { Value = blobValue };
command.Parameters.Add(fileNameParam);
command.Parameters.Add(fileParam);
try
{
connection.Open();
command.ExecuteNonQuery();
MessageBox.Show(fileNameParam.Value.ToString() + " saved to database.", "BLOB Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
connection.Close();
}
Retrieving a BLOB from the database
To retrieve a BLOB value from the database we use FileStream and BinaryWriter classes. The next example will show you the process of retrieving a BLOB from a database.
Note. you will see that we set the CommandBehavior to SquentialAccess when we call the ExecuteReader() method, this allows us to use the GetBytes() method of the SqlDataRader, so we can read the BLOB from the database in smaller, user-definable amounts.
string SavePath = @ "D:\\My BLOBs";
SqlConnection SaveConn = new SqlConnection("Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI");
SqlCommand SaveCommand = new SqlCommand();
SaveCommand.CommandText = "Select BlobFileName, BlobFile from BlobsTable where BlobFileName = @BlobFileName";
SaveCommand.Connection = SaveConn;
SaveCommand.Parameters.Add("@BlobFileName", SqlDbType.NVarChar).Value = "My Movie.wmv";
//the index number to write bytes to
long CurrentIndex = 0;
//the number of bytes to store in the array
int BufferSize = 100;
//The Number of bytes returned from GetBytes() method
long BytesReturned;
//A byte array to hold the buffer
byte[] Blob = new byte[BufferSize];
SaveCommand.Connection.Open();
//We set the CommandBehavior to SequentialAccess
//so we can use the SqlDataReader.GerBytes() method.
SqlDataReader reader = SaveCommand.ExecuteReader(CommandBehavior.SequentialAccess);
while (reader.Read()) {
FileStream fs = new FileStream(SavePath + "\\" + reader["BlobFileName"].ToString(), FileMode.OpenOrCreate, FileAccess.Write);
BinaryWriter writer = new BinaryWriter(fs);
//reset the index to the beginning of the file
CurrentIndex = 0;
BytesReturned = reader.GetBytes(1,
//the BlobsTable column indexCurrentIndex, // the current index of the field from which to begin the read operationBlob, // Array name to write tha buffer to0, // the start index of the array to start the write operationBufferSize // the maximum length to copy into the buffer);
while (BytesReturned == BufferSize) {
writer.Write(Blob);
writer.Flush();
CurrentIndex += BufferSize;
BytesReturned = reader.GetBytes(1, CurrentIndex, Blob, 0, BufferSize);
}
writer.Write(Blob, 0, (int) BytesReturned); writer.Flush(); writer.Close(); fs.Close();
}
reader.Close();
SaveCommand.Connection.Close();
To fully understand the concept you need to try to write this code yourself.
Note. The database and the full source code in the source code area with this article.

Habibur RahmanPosted Apr 19, 2021, 5:41 AM
Can we get this process dot net web application?
joseph blanchardPosted Dec 14, 2016, 9:32 PM
@blob_filename should be @blobfilename
akshay dhanjieditedPosted Feb 1, 2013, 7:22 AMEdited Feb 1, 2013, 7:23 AM
Retrieve a BLOB value from database throws an error atSqlDataReader reader = SaveCommand.ExecuteReader(CommandBehavior.SequentialAccess);error says: Must declare the scalar variable "@blob_filename". please reply me with the solution....
komalPosted Oct 1, 2012, 6:43 AM
Thank's..
alan hernandezPosted Jun 2, 2011, 1:48 AM
Im Alan from mexico, Im developing a c# windows aplication for a proyect of my school. I use MySql 5.1 how i can do the things in this article but with mysql??? I tried changing all the SqlCommands to MySqlCommand but doesn´t work.... the error says (BlobFileName cannot be null) I check all the code but i cant uderstand where is the mistake???. Greetz!! and please hellppp me!! :) ps: sorry for my bad english
bharath reddyPosted Mar 3, 2011, 9:37 AM
cool