Today in my current project we are supposed to build an image from Binary data stored in a SQL Server column. AS we know SQL Server allows us to store BLOB (Binary Large Objects) data. Here in our case the situation is as below.
In Source System data (read as Images,Videos or documents(.xls,.ppt,.pptx,.xlsx,.doc,.docx) )is stored in a column defined as VARBINARY(MAX). To the destination System which is on a seperate SQL Server instace , the source data is passed on a daily basis. The destination system is connected to a .NET application which is used as a front end. Our task is to reconstruct the same file through this binary data using this .NET application. We have used the following code snippet to achieve this functionality.
Byte[] bytImage=null;
string
SqlConnection myconn = new SqlConnection(constring);
command.Connection = myconn;
myconn.Open();
SqlDataReader dr = command.ExecuteReader();
MemoryStream ms = new MemoryStream(bytImage);
System.Drawing.Bitmap BMP = new System.Drawing.Bitmap(ms);
BMP.Save(“C:\\Temp\\Test.bmp”);
//To save a file in JPEG format
System.Drawing.Image img = new System.Drawing.Bitmap(ms);
I hope this article will help you to retrieve the binarydata from SQL server and create the corresponding documents from the same.