Click here to Skip to main content
12,627,377 members (35,134 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: SQL Server Microsoft
I want to know how can I add image to table in MS SQL Server
Posted 31-Mar-11 22:03pm
Updated 31-Mar-11 22:08pm
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

1) Bring up SQL Server Management Studio.
2) Connect, and open the database you want to add the image to.
3) Expand the tables, and either add a new table with an appropriate index field, or right click teh table and select design.
4) Add a field called "myImage", and make its datatype "image". Allow nulls.
5) Close SQL Server Management studio.

To add images to the table (I will assume a new record, the table has an Identity field, and just teh myImage column):
using (SqlConnection con = new SqlConnection(connectionString))
    {
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myImage) VALUES (@IM)", con))
        {
        byte[] imageData = File.ReadAllBytes(@"F:\Temp\Picture.jpg");
        com.Parameters.AddWithValue("@IM", imageData);
        com.ExecuteNonQuery();
        }
    }
  Permalink  
Comments
ishwar26 28-Feb-13 2:04am
   
what do i put in place of "@IM"??
OriginalGriff 28-Feb-13 3:10am
   
You don't - it ties the parameter you add in the AddWithValue to the table. The raw image data bytes go in the parameter line.
ishwar26 28-Feb-13 3:29am
   
ok, i think i have inserted the picture successfully... its showing "binary data" in the data base. thnx... now how do i proceed further? how do I retrieve it and show it on the form? wot control do i use?
Also, if i am not wrong, i have inserted the actual image rather than the path..wont this slow down the database...?
OriginalGriff 28-Feb-13 3:40am
   
Hijacking a thread is rude - you should ask this in a new question.
ishwar26 28-Feb-13 3:59am
   
I have spent hours finding a solution to my prob... cant spend time finding the rt. thread nw... may be u cud provide me the link to it ;)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

You can create a field of the datatype 'image'. That will be represented by a byte[] in your programming code.

So if you have an image, open it with a stream and convert it to a byte[]. Then add that to your SQL Table.

If you're going to add a lot of images however, I'd recommend to store the location (e.g. c:\path\to\image.jpg) in your database instead, because storing the image itself in the database may slow your DB down dramaticly.

Have fun!
Eduard
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 1 Apr 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100