Introduction
This is a step-by-step tutorial for uploading or adding an image directly into the database and viewing any uploaded image in the PictureBox control in Windows Forms Application.
In VB 6.0 there is no way of saving the binary large object (BLOB) into the database pro-grammatically, it only has the option to show the binary image from database to PictureBox. But over here in this article we will use MemoryStreamobject from System.IO base class to copy an image from the db to directly into the PictureBox control.
In order to perform this tutorial I have used following
- Microsoft Visual Studio 2010
- Microsoft SQL Server 2008 R2 (you can also use the MS Access for this)
Also I can assume that most of guys knows the Visual C# .NET Windows Forms applications, Binary Large Object(BLOB) in databases, ADO.NET data access.
Example
- As a first step we have to create a table for storing and retrieving our image(s). So let's create the table first using following query
Note: For SQL Server users please select/create a database before you run this query

Creating Table
- Next step would be opening the Visual Studio and creating the Windows Forms application project

Creating Project
- Once the project is created the then default windows form will appear in the design mode. Over here you have to add to buttons and one picture box control using ToolBox toolbar. I have renamed the form to BLOBTestForm. After the design is complete it should ideally look like this

Form Design
Note: I have added a small label for displaying some messages. As of now it not visible, once we will the program and do any action it will show you the appropriate message.
- Once your form design is complete then in the code behind we have to put some code to make it work. We will start with using statements as follows
- As database is involved we have make the connection string as well. Following declaration we can put it inside thepublic class BLOBTestForm : System.Windows.Forms.Form class declaration, configure the connection string as per your settings.
- Now all necessary things are done, it's time to add some real code on the Button 1(File to Database) click event. This code will read the local image file using FileStream object and then convert it into byte array and pass it to the database using parameterized Command object. Change the file path as necessary.
- Now we have added code for file to database, now we will add the code for database to picture box control. You can add the following code to Button 2(Database to PictureBox) click event. This piece of code retrieves the image bytes from tblBLOB table in the database and puts it into the DataSet object, copies the recently uploaded image into byte array and then converts that into the MemoryStream array, finally we will load this MemoryStream into the image property of PictureBox control.
- Coding is done, let's compile and Run this application, to do so press F5.
- Once the our Form will appear you first click on File to Database button. So this will add the file into the database. It will look like this

File to database
- Once the label shows successful message then you can click on the Database to PictureBox button. This will load the recently added image from the database to PictureBox control. The final output will look like this

Database to PictureBox