Help with managing files in a database, using TADOTable and TDataSource.

I'm using Delphi 7, and i am writing a mail client.
I need an example code, showing how to manage files in a MS Access Database, using those two components.
What do i need to use? May be an OLE Object?
bbh2k3Asked:
Who is Participating?
 
geobulConnect With a Mentor Commented:
Hi,

The example below uses files from disk and a table with one OLE Object field named 'filebin'.

// adds one file to the table
procedure TForm1.btnSaveClick(Sender: TObject);
begin
  if OpenDialog1.Execute then begin
    with ADOTable1 do begin
      Open;
      Append;
      FieldByName('id').AsString := Edit1.Text; // id - string
      FieldByName('filename').AsString := OpenDialog1.Filename; // original filename - string
      TBlobFIeld(FieldByName('filebin')).LoadFromFile(OpenDialog1.Filename); // the content of the file
      Post;
      Close;
      ShowMessage('Done');
    end;
  end;
end;

// extracts the first file to disk
procedure TForm1.btnGetClick(Sender: TObject);
var
  s: string;
begin
  if SaveDialog1.Execute then begin
    s := Edit1.Text;
    with ADOTable1 do begin
      Open;
      First; // you'll have to replace this line with Locate or similar method for finding thhe record of interest
      TBlobFIeld(FieldByName('filebin')).SaveToFile(SaveDialog1.Filename);
      ShowMessage('Done');
      Close;
    end;
  end;
end;

The example doesn't contain errors handling at all.

Regards, Geo
0
 
Wim ten BrinkSelf-employed developerCommented:
How do you want to do that? What do you mean by managing files? If you want to store emails in your database, then store them in their raw format, which happens to be ANSI-text. Also, use a TADOQuery instead since it offers a better performance.
0
 
geobulCommented:
Hi,

Perhaps you are talking about managing tables not files. If so, what kind of management you are interested in? Be more specific please.

Regards, Geo
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
kretzschmarCommented:
nothing to append ...  waiting for further info
0
 
Eddie ShipmanAll-around developerCommented:
I would think he means file attachments and how to post them to an Access database blob field.
0
 
Wim ten BrinkSelf-employed developerCommented:
Well, even file attachments are part of the raw email. They're just encoded as strings. All you need is a memo field... (Unless you're not retrieving emails in their raw format.)
0
 
Eddie ShipmanAll-around developerCommented:
Maybe he is decoding them and wants the capability to save the decoded file?

So bbh2k3, would you please enlighten us on your requirements?
0
 
bbh2k3Author Commented:
I want to store the email as ANSI text, i want to know how to store the attachment in a blob field, and extract it later.
0
 
geobulCommented:
If you also want to remove the file from the table use:

TBlobFIeld(ADOTable1.FieldByName('filebin')).Clear;

but in that case it would be necessary to check if that field is empty before saving it to a file this way:

if not ADOTable1.FieldByName('filebin').IsNull then ...

Hope this will help you.

Best regards, Geo
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.