DelphiFAQ Home Search:

Connect to an Access Database with ADO and write a BlobStream value

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

How can I connect to an Access Database with ADO and write a BlobStream value?

Answer:

Use the functions from below: ConnectToADODB() to connect to the database and UpdateBlob() to update your data.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ADODB, DB, DBTables, ComObj;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function ConnectToADODB(var Query: TADOQuery; ConnectStr: string) : boolean;
overload;

function UpdateBlob(Connection: TADOConnection; Spalte: string; Tabelle: string;
                    Where: string; var ms: TMemoryStream) : boolean;

procedure ShowEOleException(AExc: EOleException; Query: string);

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Query: TADOQuery;
  ms: TMemoryStream;
  ConnectStr: String;
begin { TForm1.Button1Click }
  ms := TMemoryStream.Create;
  ms.LoadFromFile('d:\a.txt');
  Query := TADOQuery.Create(nil);

  // You must connect to AccessDB first.
  // See: Query.Connection, TADOConection or Query.ConnectionString
  //my function to connect to DB
  ConnectStr := 'Provider=Microsoft.Jet.OLEDB.4.0;'+// provider for Access2000
  'Data Source=C:\db1.mdb;'+// databasefile
  'Mode=ReadWrite|Share Deny None;'+// set to ReadWrite
  'Persist Security Info=False';
  if not ConnectToADODB(Query, ConnectStr) then
    ShowMessage('Connecting to DB failed.');

  // data is my row and email the table
  UpdateBlob(Query.Connection, 'blobfieldname', 'Tabelle1',
             'id=1', ms);
  ms.Free;

  // disconnect from DB
  Query.Connection.Close;

  Query.Free;
end; { TForm1.Button1Click }

function ConnectToADODB(var Query: TADOQuery; ConnectStr: string) : boolean;
overload;
begin { ConnectToADODB }
  Query.Connection := TADOConnection.Create(nil);
  Query.Connection.LoginPrompt := true;
  Query.Connection.ConnectionString := ConnectStr;
  Query.Connection.Open;
  Result := Query.Connection.Connected;
end; { ConnectToADODB }

function UpdateBlob(Connection: TADOConnection; Spalte: string; Tabelle: string;
                    Where: string; var ms: TMemoryStream) : boolean;
var
  BlobField: TBlobField;
  Table: TADOTable;
begin { UpdateBlob }
  Result := true;
  try
    ms.Seek(0, soFromBeginning);
    Table := TADOTable.Create(nil);
    Table.Connection := Connection;
    Table.TableName := Tabelle;
    Table.Filtered := false;
    // Set Filter like SQL-Command '... WHERE id=1'
    Table.Filter := Where;
    Table.Filtered := true;
    Table.Open;
    Table.First;

    if not Table.FieldByName(Spalte).IsBlob then
      raise EOleException.Create('The field '+Spalte+' is not a blob-field.',
                                 S_FALSE, 'ITSQL.UpdateBlob',
                                 '', 0);

    BlobField := TBlobField(Table.FieldByName(Spalte));
    Table.Edit;
    BlobField.LoadFromStream(ms);
    Table.Post;
    Table.Free;
  except
    on E : EOleException do
      begin
        ShowEOleException(E, 'UPDATE BLOB FROM: SELECT '+Spalte+
                          ' FROM '+Tabelle+' WHERE '+Where);
        Result := false;
      end;
  end; { try }
end; { UpdateBlob }

procedure ShowEOleException(AExc: EOleException; Query: string);
var
  ErrShowFrm: TForm;
  Memo: TMemo;
begin { ShowEOleException }
  ErrShowFrm := TForm.Create(nil);
  ErrShowFrm.Position := poScreenCenter;
  ErrShowFrm.Width := 640;
  ErrShowFrm.Height := 480;
  Memo := TMemo.Create(ErrShowFrm);
  Memo.Parent := ErrShowFrm;
  Memo.Align := alClient;

  Memo.Lines.Clear;
  Memo.Lines.Add('Message: '+AExc.Message);
  Memo.Lines.Add('   Source: '+AExc.Source);
  Memo.Lines.Add('   ClassName: '+AExc.ClassName);
  Memo.Lines.Add('   Error Code: '+IntToStr(AExc.ErrorCode));
  Memo.Lines.Add('   Query: '+Query);

  ErrShowFrm.ShowModal;
  Memo.Free;
  ErrShowFrm.Free;
end; { ShowEOleException }

end.

Comments:

2008-04-07, 10:19:57
anonymous from Iceland  
I am biologist and a farmer in Iceland.

I need help. My problem is that I am triing to connect Delphi BDE to a Microsoft Access database on my local PC. I do not use username or password when I work in that database. Newertheless, when I try to connect my Delphi to the database the computer asks for both username and password. I use Delphi 6 program which I got 5 years ago when I teached programming in a college.



Can you help me?



Regards,



Tomas Isleifsson

2008-05-31, 08:35:37
anonymous from Czech Republic  
try - in TAdoConnection set property LoginPrompt = false

Bye

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: