Click here to Skip to main content
15,867,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,

How to insert image in database .

i am using ASP.NET,C#,SQL2005 and Microsoft Visual
Studio 2008.
Posted

I implemented this using the following code it might also help you

Storing and Retrieving Images from SQL Server using Microsoft .NET[^]

Save An Image Into SQL Server 2000 Database[^]
 
Share this answer
 
I only move the image file to an folder, and save the image url in the database, to show the image, just go to the database fetch the url!
 
Share this answer
 
C#
 byte[] ImageBytes;
void ImageinBytes()
{
if (!string .IsNullOrEmpty ( FileName)) 
            {
                 Bitmap BitMap1 = new Bitmap(FileName);
                 System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream();
                BitMap1.Save(MemoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                ImageBytes = MemoryStream.ToArray();
            }
}



here FileName is complete path of File.



For extracting Bytes in Image again

C#
void CovertingInImage()
        {
            System.IO.MemoryStream MemoryStream = new System.IO.MemoryStream(ImageBytes);
            Bitmap BitMap2 = new Bitmap(MemoryStream);
            Image Image1;
            Image1 = Image.FromStream(MemoryStream);
        }


Here imagebytes is
 
Share this answer
 
Comments
Prasad_Kulkarni 14-May-12 5:38am    
Good answer +5!
You need to pass 'image in binary format' or 'folder location where image is stored' to your database using ADO.NET! Read about ADO.NET here and it will help you.

Look at these:
MSDN: ADO.NET[^]
MSDN: Accessing Data with ADO.NET[^]


Following articles for reference, if needed:
Uploading and Storing Image Path to Database and Image to Folder - Part 1[^]
Uploading and Storing Image Path to Database and Image to Folder - Part 2[^]
Retrieving Image from SQL Database in C#[^]
 
Share this answer
 
v2
Comments
Prasad_Kulkarni 14-May-12 5:37am    
Surely a 5!
Sandeep Mewara 14-May-12 8:27am    
Thanks Prasad.
Inserting Image into database

1st Method: Store image in your server and pass the imageurl(stored image file path) to data base

2nd method :First Convert image into byte format then store into database(use the blob datatype in your table).
 
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