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 an image into table using TQuery
Creating an image using TImage
.
msBinImgStream := TMemoryStream.Create;
imgCustom := TImage.Create(self);
imgJpg := TJPEGImage.Create;
Converting the image to TJpegImage
and saving to TMemoryStream
.
imgJpg.Assign(imgCustom.Picture.Bitmap);
imgJpg.SaveToStream(msBinImgStream);
Inserting into the database by using SetBlobdata
property of TQuery
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;
Now doing the same thing using TADOQuery
:
- Able to create the image.
- Converting it to TJpeg and saving to
TMemoryStream
.
Trying to insert the image into database using LoadFromStream(stream, ftBlob)
but getting an 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;
Kindly let me know, with this approach how should I overcome this issue.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…