Hello, Audience In this tutorial we are going to see how we can Save and Retrieve Image in Database in C# Windows Application.
How to Save and Retrieve Image from Database in C# Windows Application?
Suppose you have a situation to save Image of Student into the database in a Desktop application. Here I am showing a simple example of such type of application.
Step 1- Create A Student Table.
Step 2- Open Visual Studio and Create a C# Windows Application with the name DebugDatabaseFileSave.
Step 3- Drag one Label and one Textbox on Windows Form and change the Text property of Label to Student Name and Change the Name property of the TextBox to txtStudentName.
Step 4-Drag one Label, one TextBox and a Button. Change Text property of the Label to Student Photo, Name property of the TextBox to txtStudentPhoto and Name property of the Button to btnBrowse and Text Property to Browse.
Step 5- Drag one more button that will execute the Save method, Change the Name property of the button to btnSave and Text property to Save.
Step 6- Drag an OpenFileDialog Control on the Windows Form Area.
Your Windows Form should look like this.
Step 7- Drag a DataGridView on the Windows Form and Change the Name to dgvData.
Step 8- Now add Column to the DataGridView as shown in below image.
Step 9- Add a Column of type TextBox to DataGridView for showing Student Name as shown in below image.
Step 10- Add another column to the DataGridView for showing Student Photo. Add a Column Image to the DataGridView as shown in the below Image.
Now your Windows Form should look like this.
Step 11- Add following namespaces in your code-behind file.
Step12- Now, first of all, we need to add functionality for Browse Button. Double Click the browse button and write the following code.
Step 13- To get bytes for the Selected file write the following method.
Step 13- Now Double Click on Save button and write the following code.
Step 13- To Bind Data to the DataGridView write the following method.
Complete Code for the application:
Now run the application and save the record. Here is my output window.
Hope you understand how to save and retrieve image from database in C# Windows Application. I will appreciate your feedback, Comments and Suggestions.