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, BinaryWriter which exists 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";
-
-
- FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
-
-
- BinaryReader reader = new BinaryReader(fs);
-
-
- byte[] BlobValue = reader.ReadBytes((int) fs.Length);
-
- fs.Close();
- reader.Close();
-
- SqlConnection BlobsDatabaseConn = new SqlConnection("Data Source = .; Initial Catalog = BlobsDatabase; Integrated Security = SSPI");
- SqlCommand SaveBlobeCommand = new SqlCommand();
- SaveBlobeCommand.Connection = BlobsDatabaseConn;
- SaveBlobeCommand.CommandType = CommandType.Text;
- SaveBlobeCommand.CommandText = "INSERT INTO BlobsTable(BlobFileName, BlobFile)" + "VALUES (@BlobFileName, @BlobFile)";
-
- SqlParameter BlobFileNameParam = new SqlParameter("@BlobFileName", SqlDbType.NChar);
- SqlParameter BlobFileParam = new SqlParameter("@BlobFile", SqlDbType.Binary);
- SaveBlobeCommand.Parameters.Add(BlobFileNameParam);
- SaveBlobeCommand.Parameters.Add(BlobFileParam);
- BlobFileNameParam.Value = filePath.Substring(filePath.LastIndexOf("\\") + 1);
- BlobFileParam.Value = BlobValue;
- try {
- SaveBlobeCommand.Connection.Open();
- SaveBlobeCommand.ExecuteNonQuery();
- MessageBox.Show(BlobFileNameParam.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 {
- SaveBlobeCommand.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 to a database.
NOTE: you will see that we set the CommandBehavior to SquentialAccess when we call 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";
-
-
- long CurrentIndex = 0;
-
-
- int BufferSize = 100;
-
-
- long BytesReturned;
-
-
- byte[] Blob = new byte[BufferSize];
-
- SaveCommand.Connection.Open();
-
-
-
- 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);
-
-
- CurrentIndex = 0;
-
- BytesReturned = reader.GetBytes(1,
-
- 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.