Rate this:
See more:
I want to know how can I add image to table in MS SQL Server
Posted 31-Mar-11 21:03pm
Edited 31-Mar-11 21:08pm

Rate this:

Rate this:

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
Rate this:

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))
{
com.ExecuteNonQuery();
}
}

ishwar26 at 28-Feb-13 2:04am

what do i put in place of "@IM"??
OriginalGriff at 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 at 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 at 28-Feb-13 3:40am

Hijacking a thread is rude - you should ask this in a new question.
ishwar26 at 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 ;)

 0 Kornfeld Eliyahu Peter 430 1 OriginalGriff 304 2 Nelek 115 3 Sergey Alexandrovich Kryukov 99 4 Gihan Liyanage 99
 0 Kornfeld Eliyahu Peter 420 1 OriginalGriff 140 2 Gihan Liyanage 99 3 Nelek 65 4 sankarsan parida 65

Web04 | 2.8.140827.1 | Last Updated 1 Apr 2011