Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
3.80/5 (4 votes)
See more:
How to store and Retrieved image in sql database
Posted
Updated 20-Jun-12 21:20pm
v2

Depending on the size of the image, I wouldn't recommend storing it in the DB directly - small thumbnail sizes are ok, but images these days get big pretty fast, and can take up a lot of SQL server bandwidth and DB space. If you are looking at storing a lot of largish images, I would create a folder for them, and store them under a temporary name, recording that in the DB instead. Personally, I store a thumb in the DB, with a path reference to the full image.

However, it isn't difficult to store or retrieve images:
C#
using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("INSERT INTO Images (Id, Location, ImageData, InsertDate) VALUES (@ID, @DS, @TN, @DI)", con))
        {
        cmd.Parameters.AddWithValue("@ID", Id);
        cmd.Parameters.AddWithValue("@DS", Location);
        cmd.Parameters.AddWithValue("@TN", myImage.ToByteArray());
        cmd.Parameters.AddWithValue("@DI", InsertDate);
        cmd.ExecuteNonQuery();
        }
    }

C#
using (SqlConnection con = new SqlConnection(GenericData.DBConnection))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT ImageData FROM Images WHERE Id=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", Id);
        SqlDataReader r = cmd.ExecuteReader();
        if (r.Read())
            {
            MemoryStream ms = new MemoryStream((byte[])r["ImageData"]);
            MyImage = Image.FromStream(ms);
            }
        }
    }
 
Share this answer
 
Comments
Manas Bhardwaj 21-Jun-12 4:48am    
V.g. +5!
Sunny_Kumar_ 21-Jun-12 7:33am    
Great show. my +5!
Prosan 21-Jun-12 7:39am    
great my 5
Jαved 22-Jun-12 2:09am    
Good +5.
OriginalGriff 28-Nov-12 1:59am    
As I said elsewhere, open a new question - we need to know about your situation, not something that may be similar to someone elses.
Refer:
A sample program to demonstrate how to save or store images in SQL server
Store or Save images in SQL Server[^]

This article is about storing and retrieving images from database in Microsoft .NET using C#.
Storing and Retrieving Images from SQL Server using Microsoft .NET[^]

You can also try search on google[^] or CodeProject[^]
 
Share this answer
 
Comments
Manas Bhardwaj 21-Jun-12 4:47am    
Correct +5
Prasad_Kulkarni 21-Jun-12 4:51am    
Thank You Manas! :)
use 'BLOB' concept to store and retrieve Images.

A very good example is dotnet_read_write_blob_Example .

go through the link and you might be able to find out solution for your query.
 
Share this answer
 
 
Share this answer
 
C#
public void get ()
{
ljhsakfsdjf;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900