We help IT Professionals succeed at work.

sql query in text

JCWEBHOST used Ask the Experts™
hey guys i have a problem.

i can not insert a image to my database using text query?


i have a sql = (insert (img)values((object)(Session["image"]))

do not work, couse i pass the text sql to a method

please help?
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

What is the client that runs this SQL text?

In straight TSQL you cannot do this unless it is a binary stream, so the client usually handles putting the data into a binary stream.

NOTE: If you are on SQL 2005, in the future the type "image" is deprecated and will be removed from the product.  I would advise using varbinary(max) instead.  The insert is still the same.
Ryan McCauleySenior Data Architect

I'd do it using a Parameter on you SqlCommand object that contains the binary list from your Session State - that's what has worked well for me, and it's also a bit easier to read, since it separates the insert itself from the content.
1) insert into xyz(img) values ((object)(Session["image"]))

2)  you must need binary stream data to insert inside database...

 //Open file into a filestream and
 //read data in a byte array.
 byte[] ReadFile(string sPath)
 //Initialize byte array with a null value initially.
 byte[] data = null;

 //Use FileInfo object to get file size.
 FileInfo fInfo = new FileInfo(sPath);
 long numBytes = fInfo.Length;

 //Open FileStream to read file
 FileStream fStream = new FileStream(sPath, FileMode.Open,

 //Use BinaryReader to read file stream into byte array.
 BinaryReader br = new BinaryReader(fStream);

 //When you use BinaryReader, you need to

 //supply number of bytes to read from file.
 //In this case we want to read entire file.

 //So supplying total number of bytes.
 data = br.ReadBytes((int)numBytes);