2013-02-03 180 views
2

我存儲一些信息到我的Access數據庫,一個參數是BLOB字段,在這種情況下是一個圖像,加載到Timage。保存圖像到記錄

我使用這段代碼保存它:

var 
AStream : TMemoryStream; 
begin 
AStream := TMemoryStream.Create; 
    try 
Image1.Picture.Graphic.SaveToStream(AStream); 
AStream.Position := 0; 
if Adotable1.Active then 
begin 
    Adotable1.Edit; 
    TBlobField(Adotable1.FieldByName('Termograma')).LoadFromStream(AStream); 
    Adotable1.Post; 
end; 
    finally 
    AStream.Free; 

adotable1.Append; 
adotable1['Data']:= datetimepicker1.Date; 
adotable1['Temax']:= edit4.Text; 
adotable1['Temin']:= edit5.Text; 
adotable1['Descrição da Posição']:= memo1.Text; 
adotable1['Comentários']:= memo2.Text; 
adotable1.Post; 

但我也有我通過點擊像「追加」部分相同的按鈕存儲的其他信息。

會發生什麼情況是,當我按下保存按鈕時,該信息不會被存儲在數據庫的相同ID中。

如何解決此問題?

回答

5

您正在編輯當前記錄,將圖像保存到它,追加一條新記錄,並將其餘信息保存到該新記錄。我想你打算添加一個全新的記錄,將圖像和數據添加到新的記錄中,然後保存這些更改。

試試這個:

var 
    AStream : TMemoryStream; 
begin 
    if not AdoTable1.Active then 
    AdoTable1.Open; 

    Adotable1.Append; 

    AStream := TMemoryStream.Create; 
    try 
    Image1.Picture.Graphic.SaveToStream(AStream); 
    AStream.Position := 0; 
    TBlobField(Adotable1.FieldByName('Termograma')).LoadFromStream(AStream); 
    finally 
    AStream.Free; 
    end; 

    adotable1['Data']:= datetimepicker1.Date; 
    adotable1['Temax']:= edit4.Text; 
    adotable1['Temin']:= edit5.Text; 
    adotable1['Descrição da Posição']:= memo1.Text; 
    adotable1['Comentários']:= memo2.Text; 
    adotable1.Post; 
end; 
+0

它的工作,謝謝:) – Ammadeux

+0

我保存這些圖像.JPEG,但我不斷收到這個「位圖圖像無效」的錯誤。我像這樣加載圖像:if OpenPictureDialog1.Execute(Self.Handle)then Image1.Picture.LoadFromFile(OpenPictureDialog1.FileName); – Ammadeux

+4

這是一個完全獨立的問題,應該作爲一個問題。 :-)這一個是關於將圖像保存到數據庫,並已被回答。 –