We help IT Professionals succeed at work.

sql query in text

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

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

(object)(Session["image"])

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

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

please help?
Comment
Watch Question

Do more with

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

Commented:
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

Commented:
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.
Commented:
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,
 FileAccess.Read);

 //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);
Session["image"]=data