![]() |
Store/Retrieve binary data (BLOBs) from database
[BLOBs - Audio, Image, Video, etc.] (Database, VB.NET)
I always wanted a simple way to store binary data (images, audio, video, EXEs, XML files, etc.) in database and retrieve it when needed? Here, I have tried to present the simplest and perhaps the shortest way of doing so in VB.NET. The Trick In single line: We use an array of bytes to get/put data into database's OLE field. There are two things to notice in the above statement... 1. The field in which we want to put/get binary data must be of OLE Object
data type. To see how to do it exactly, please see the example below. In this example, we will store/retrieve images into database. Once, you get the feeling of how to do so, you can easily store/retrieve any kind of data in database. Example
In this example, we have used an Access database with a table (Table1) which has an OLE Object field (img). Part 1 - Global variable declaration Dim strConn As String = "Provider=microsoft.jet.oledb.4.0;
data source=" & Application.StartupPath & "\..\db.mdb" DataAdapter (da): This is used to execute SQL commands at data source. In this example we have used this to execute SELECT command on our database. We will also use this object to fill our dataset object (see Part 2) DataSet (ds): This object is used to hold our table and perform operations on it. CommandBuilder (cb): This object is used to internally build SQL commands (like INSERT, UPDATE and DELETE) for the DataAdapter object for us. Therefore, we will have no need to write SQL commands for these operations! DataRow (row): This object is used to hold a row. Part 2 - Filling the DataSet da.Fill(ds) The Fill method of DataAdapter is used to fill a given DataSet with the result of the specified SQL statement (In this case, SELECT img FROM Table1).For your convenience, I have tried to explain the flow diagrammatically below:
Part 3 - Reading the file into an array Whatever you are trying to put in database must be in some file (in most of the cases). Our next step is to read the file into a byte array. See the code below: Dim arr() As Byte Line 1: Create array (check the data type - Byte) Part 4 - Saving data (array) to database row = ds.Tables(0).NewRow Line 1: Create a new row and assign it to
row object variable Part 5 - Retrieving data into array Dim arr() As Byte Line 1: Create a byte array I recommend that you download the source code and see the whole thing running if you have any confusion. I am sure that will help you. Part 6 - Saving array to file FileOpen(1, "filename", OpenMode.Binary) I would like to mention one thing here. If we are dealing with images and the data retrieved from database into array is an image, we can show that image into a PictureBox ( picLoad) using the following line:picLoad.Image = picLoad.Image.FromStream(New IO.MemoryStream(arr)) I hope this code is simple enough and will definitely help you. If you need to discuss anything related to this, please do not hesitate to contact me at rahul@coder000.com, I will be happy to discuss it further. Happy coding!! |