Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 07-31-2012, 06:20 AM
vishualsoft vishualsoft is offline
Senior Member
 
Join Date: Jan 2010
Location: Bangalore, India
Posts: 244
Question How to insert image into database using TADOQuery Component Only

Hi All,

I have one simple fundamental issue, I am trying to insert image into the database using Insert statement with other column values also using TADOQuery component.

Since the code is already written by somebody, some dummy sample code I would like to put here for your clarification with the respective steps.

Please note that this was working fine with TQuery component, since I am replacing TQuery with TADOQuery component, I must do the same using TADOQuery component only.

The same code should work for SQL Server as well as Oracle databases.

The datatype of the column in which I am trying to insert the image is of type VarBinary in SQL Server database.

Inserting a image to table using TQuery

1. Creating an image using TImage.

Code:
 
   msBinImgStream  := TMemoryStream.Create;
   imgCustom        := TImage.Create(self);
   imgJpg              := TJPEGImage.Create;

2. Converting the image to TJpegImage and saving to TMemoryStream.

Code:
 
  imgJpg.Assign(imgCustom.Picture.Bitmap);
  imgJpg.SaveToStream(msBinImgStream);
3. Inserting into the database by using SetBlobdata property of TQuery component.

Code:
 
  sSql := 'INSERT INTO Table_Name(Column1, Column2, Column_Image) VALUES ( ''' + Value1 + ''', ''' + Value2 + ''', :pBlob)';

  qryTQuery.SQL.Add(sSQL);

  qryTQuery.ParamByName('pBlob').SetBlobData(msBinImgStream.Memory, msBinImgStream.Size);

  qryTQuery.ExecSQL;

Now doing the same thing using TADOQuery
1. Able to create the image.
2. Converting it to TJpeg and saving to TMemoryStream.
3. Trying to insert the image into database using LoadFromStream(stream, ftBlob) but getting an error "String or binary value may be truncated".


Code:
 
  sSql := 'INSERT INTO Table_Name(Column1, Column2, Column_Image) VALUES ( ''' + Value1 + ''', ''' + Value2 + ''', :pBlob)';
 
  qryADOQuery.SQL.Add(sSQL);

  qryADOQuery.Parameters.ParamByName('pBlob').LoadFromStream(msBinImgStream, ftBlob);

  qryADOQuery.ExecSQL;


Kindly let me know, with this approach how should I overcome this issue.



Thanks In Advance.


With Best Regards.

Vishal
__________________
"Hardwork always pays."

"Confidence never comes if we have all the answers
but it comes only when we are ready for all the questions."

The man who removes a mountain begins by carrying away small stones. - William Faulkner
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 05:14 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.