Delphi實現(xiàn)在數(shù)據(jù)庫中存取圖像

字號:

本實例演示如何在數(shù)據(jù)庫中存取圖像文件。
    向窗體上添加一個TListBox組件、一個TImage組件和一個TTable組件,設(shè)計完成的主界面。
    本系統(tǒng)中需要設(shè)計一個新的基于Paradox 7的數(shù)據(jù)庫Image.db,圖2為設(shè)計完成的Image.db數(shù)據(jù)庫。
    為了方便測試程序,Image.db數(shù)據(jù)庫存儲在實例程序所在的路徑下。
    設(shè)置TTable組件的TableName屬性為Image.db,Active屬性為True。
    在程序運行初期,首先會判斷Image.db數(shù)據(jù)庫中是否存在記錄,如果沒有記錄存在,那么就執(zhí)行以下代碼向Image.db數(shù)據(jù)庫中添加“鳥.bmp”文件:
    procedure TForm1.FormCreate(Sender: TObject);
    var
    mem:TMemoryStream;
    begin
    if Table1.Eof and Table1.Bof then
    begin
    with Table1 do
    begin
     Insert;
     FieldByName(’Name’).AsString:=’鳥’;
     mem:=TMemoryStream.Create();
     mem.LoadFromFile(’鳥.bmp’);
     TBlobField(FieldByName(’Data’)).LoadFromStream(mem);
     Post;
    end;
    end;
    end;
    然后按照相同的方式順序向Image.db數(shù)據(jù)庫中添加“樣品.wav”、“葉子.wav”和“荷花”圖像文件。
    最后通過下面的代碼把Image.db數(shù)據(jù)庫中存儲的文件名字添加到窗體的TListBox組件中:
    with Table1 do
    begin
    First;
    while not Eof do
    begin
    ListBox1.Items.Add(FieldByName(’Name’).AsString);
    Next;
    end;
    end;