I have one simple fundamental problem: I am trying to insert an image into a database using the Insert statement with different column values, also using the TADOQuery component.
Since the code has already been written by someone, some dummy sample code that I would like to put here for clarification with the appropriate steps.
Note that this works fine with the TQuery component, since I am replacing TQuery component, I have to do the same with the TADOQuery component.
The same code should work for both SQL Server and Oracle databases.
The data type of the column in which I am trying to insert an image is of the VarBinary type in the SQL Server database.
Insert an image into a table using TQuery
Creating an image using TImage .
msBinImgStream := TMemoryStream.Create; imgCustom := TImage.Create(self); imgJpg := TJPEGImage.Create;
Convert image to TJpegImage and save to TMemoryStream .
imgJpg.Assign(imgCustom.Picture.Bitmap); imgJpg.SaveToStream(msBinImgStream);
Insert into the database using the SetBlobdata property of the SetBlobdata component.
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;
Doing the same using TADOQuery :
- Ability to create images.
- Convert it to TJpeg and save to
TMemoryStream . Trying to insert an image into the database using LoadFromStream(stream, ftBlob) , but get the error "String or binary value may be truncated."
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;
Please let me know with this approach, how can I solve this problem.
source share