Objective
To insert & retrieve images from SQL server database using stored procedures and also to perform insert, search, update, and delete operations & navigation of records.
Introduction
As we want to insert images into the database using stored procedures, we have to create a table and stored procedures in the database.
Query for creating a table in our application.
Stored procedures
Design
![Design]()
Design the form as above with 1 PictureBox control, 1 OpenFileDialog control, 4 Labels, 4 TextBoxes, and 11 Buttons.
PictureBox1 Properties
Note that OpenFileDialog control appears below the form (not on the form).
Introduction to code
In order to communicate with the SQL server database, including the namespace using System.Data.SqlClient.
In this application, we will search for a record by taking input from the InputBox. For this, we have to add a reference to Microsoft.VisualBasic.
Adding a Reference to 'Microsoft.VisualBasic'
Goto Project Menu ->Add Reference -> select 'Microsoft.VisualBasic' from the .NET tab.
In order to use this reference, we have to include the namespace, 'using Microsoft.VisualBasic' in the code.
Converting an image into binary data
We can't store an image directly into the database. For this, we have two solutions.
- To store the location of the image in the database
- Convert the image into binary data insert that binary data into the database and convert that back to the image when retrieving the records.
If we store the location of an image in the database and suppose that image is deleted or moved from that location, we will face problems while retrieving the records. So it is better to convert an image into binary data and insert that binary data into the database and convert that back to an image while retrieving records.
We can convert an image into binary data using 1. FileStream (or) 2. MemoryStream
1. FileStream uses file location to convert an image into binary data which we may/may not provide when updating a record.
Ex
2. So it is better to use MemoryStream which uses the image in the PictureBox to convert an image into binary data.
Ex
In order to use FileStream or MemoryStream we have to include the namespace: 'using System.IO'.
OpenFileDialog Control
We use OpenFileDialog control to browse for the images (photos) to insert into the record.
Using DataAdapter with StoredProcedures
We can use the command object to work with stored procedures bypassing the stored procedure name to the command object and specifying CommandType as StoredProcedure.
Ex
DataAdapter can't interact directly with the stored procedures, but if we need to use DataAdapter while working with stored procedures, we can achieve this by passing the command object to the DataAdapter.
Loading the constraint details into the dataTable
In this app., we use the Find() method to search a record, which requires details of a primary-key column, which can be provided using the statement.
Pointing to the current record in Table
After searching for a record, we have to get the index of that record so that we can show the next and previous records when we press the'>>'(next) and '<<'(previous) buttons.
Ex
Code