0

I have a table like this.

 myID   Name     Pic
   1     Shan    
   2     Sharon 

myId column is integer type, Name is varchar(10) and pic is type of image data type. I'm using SQL Server.

In a normal insert procedure, in which if you don't have a column name of data type "image" I know the insert would look like this.

      INSERT INTO myTable
      VALUES (1,Shan);

How to insert image data type column how to select the file. How to write an insert query to a table which is having a image data type column?

Thanks in advance.

ChathurawinD
  • 756
  • 1
  • 13
  • 35
  • @bummi column type is not binary or blob, it's image data type. – ChathurawinD Oct 14 '14 at 09:39
  • They are all stored binary [ntext, text, and image](http://msdn.microsoft.com/en-us/library/ms187993.aspx) `if (Select CAST(PICAsImage as Varbinary(max)) from PICS where ID=1) = (Select PICAsVarBinary from PICS where ID=2) print 'true'` will print true if the same image was loaded via `OPENROWSET(BULK ` – bummi Oct 14 '14 at 09:53
  • @bummi I didn't want a casted answer, I just asked a simple query, with a simple explanation if could. i don't understand what you are talking about. i just wanted to know how to set file path for the image. – ChathurawinD Oct 14 '14 at 10:00
  • It's shown in the answer to then question linked above http://stackoverflow.com/a/4711255 `image` is a deprecated fieldkind, the behavior is identic to `Varbinary (max)` – bummi Oct 14 '14 at 10:05
  • @bummi I went through that answer, why is that SELECT statement there, can you point a detailed explanation, or a tutorial which is explaining. – ChathurawinD Oct 14 '14 at 10:14
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63020/discussion-between-bummi-and-chathwind). – bummi Oct 14 '14 at 10:28

1 Answers1

4

1.Create FileStream with your image file.

2.Create byte array of length FileStream.Length.

3.use this byte array as value for your Pic column.

Here is good article on code project - http://www.codeproject.com/Articles/354639/Storing-and-Retrieving-Images-from-SQL-Server-Us

Another solution which I can think of is, you just store the absolute disk path of your image file. You just add column of nvarchar type and use this for path.

Mukund
  • 1,679
  • 1
  • 11
  • 20