Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to know how can I add image to table in MS SQL Server
Posted
Updated 31-Mar-11 21:08pm

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();
        }
    }
 
Share this answer
 
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 ;)
 
Share this answer
 
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
 
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