Click here to Skip to main content
16,005,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to store images in sql & How to retrive images from sql?
Posted

This is exactly what you want :
Storing binary data in SQL Server using EF[^]
 
Share this answer
 
Comments
edjeit 25-Sep-13 10:28am    
I don't understand why this is down voted...
And to add one more link, if you want to use FileStream in SQL Server, here's an example: How to store and fetch binary data into a file stream column[^]
 
Share this answer
 
Comments
Espen Harlinn 29-Dec-12 7:37am    
5'ed!
Wendelius 29-Dec-12 7:54am    
Thank you :)
 
Share this answer
 
Comments
Wendelius 29-Dec-12 6:01am    
Also a good link.
Thomas Daniels 29-Dec-12 6:02am    
Thank you!
SQL
CREATE TABLE [dbo].[ImageData]
 (
    [ImageID] [int] IDENTITY(1,1) NOT NULL,
    [ImageData] [image] NULL,
 CONSTRAINT [PK_ImageData] PRIMARY KEY CLUSTERED
 (
    [ImageID] ASC
 )
 WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
 ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
 )
 ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]



In this example I am going to use Four(4) Stored Procedures call ReadAllImage, ReadAllImageIDs, ReadImage, SaveImage and use below SQL scripts to create those Procedures.

SQL
CREATE proc [dbo].[ReadAllImage] as
SELECT * FROM ImageData
GO


SQL
CREATE proc [dbo].[ReadAllImageIDs] as
SELECT ImageID FROM ImageData
GO



SQL
CREATE proc [dbo].[ReadImage] @imgId int as
SELECT ImageData FROM ImageData
WHERE ImageID=@imgId
GO



SQL
CREATE proc [dbo].[SaveImage] @img image as
INSERT INTO ImageData(ImageData)
VALUES (@img)
GO



FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read);
    byte[] img = new byte[FS.Length];
    FS.Read(img, 0, Convert.ToInt32(FS.Length));

    if (con.State == ConnectionState.Closed)
      con.Open();
    SqlCommand cmd = new SqlCommand("SaveImage", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@img", SqlDbType.Image).Value = img;
    cmd.ExecuteNonQuery();
    loadImageIDs();


C#
SqlConnection con = new SqlConnection(DBHandler.GetConnectionString());
    SqlCommand cmd = new SqlCommand("ReadImage", con);
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add("@imgId", SqlDbType.Int).Value = 
              Convert.ToInt32(cmbImageID.SelectedValue.ToString());
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    try
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["ImageData"]);
            picImage.Image = Image.FromStream(ms);
            picImage.SizeMode = PictureBoxSizeMode.StretchImage;
            picImage.Refresh();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", 
              MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }
 
Share this answer
 
v2
Comments
Wendelius 29-Dec-12 6:01am    
Good example, my 5
Oleksandr Kulchytskyi 29-Dec-12 6:06am    
Thanks
Espen Harlinn 29-Dec-12 7:37am    
Well done :-D

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