DelphiFAQ Home Search:

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

 

comments23 comments. Current rating: 3 stars (21 votes). Leave comments and/ or 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:

You are on page 2 of 2, other pages: 1 [2]
2017-12-19, 18:43:12
Chelsea from Ukraine  
rating
online loans direct lenders
payday loans
money mutual
online loans
2017-12-20, 19:54:36
Gretta from Ukraine  
rating
loans for good credit
payday loans online
payday loans in az
payday loans online
2017-12-23, 03:19:22
Les from Ukraine  
rating
payday loans las vegas
quick loan
apply for payday loans
payday loans online legit
2017-12-24, 14:43:56
Moshe from Ukraine  
rating
online loans
online loans direct lenders
payday loans direct lenders
payday loans bad credit
2017-12-26, 00:26:49
Loren from Ukraine  
rating
payday loans online bad credit
payday loans online no credit check
payday loans bad credit
payday loans no credit check
2017-12-26, 13:18:42
Corine from Ukraine  
rating
payday loans direct lenders
payday loans no credit check
payday loans no credit check
loans online no credit check
2018-01-20, 14:06:51
Seth from Ukraine  
rating
usa online slots real money
best free online casino
free casino games no download no registration
usa online slots real money
free casino games and poker
2018-02-02, 17:00:08
Mamie from Ukraine  
rating
diet pills for men
diet pills for women
weight loss pills fda approved
weight loss plans
You are on page 2 of 2, other pages: 1 [2]

 

 

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: