DelphiFAQ Home Search:

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

 

comments12 comments. Current rating: 3 stars (10 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:

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
2017-08-07, 21:39:18
Rafael from France  
rating
cheapest auto insurance
[url=' https://7auto24insur..']cheapest car insurance[/url]
cheapest car insurance
cheap auto insurance online
2017-08-09, 11:59:10
Kari from France  
rating
payday loans online
[url=' https://loansmoneypa..rg/']loans online[/url]
payday loan
loans online
2017-08-10, 06:36:53
Wilfredo from France  
rating
payday loan
[url= https://loansxxonlin..rg/]payday loans[/url]
online loans
payday loan
2017-08-14, 15:40:37
Kirk from France  
rating
why auto insurance premiums increase
[url= http://assol-volsk...31732]does auto insurance cover moving trucks[/url]
top auto insurance companies 2015
can auto insurance premiums be deducted
2017-08-22, 11:18:10
Burton from France  
rating
последний рыцарь
[url= http://kinogo-film.../]кино +в качестве[/url]
кино смотреть бесплатно +в хорошем качестве
стража 2
2017-08-24, 23:28:04
anonymous from France  
rating
cheap insurance for cars
https://7auto24insurance.org/ - cheap auto insurance[/url]
auto insurance quotes
<a href=' https://7auto24insur...org/'> auto insurance quotes</a>
2017-09-02, 09:58:24
Yetta from France  
rating
https://getinsurance..wfast.org/ - auto insurance quotes
car insurance quotes
car insurance quotes
2017-10-27, 03:06:34
Jessica from Ukraine  
rating
real money casino
best online casino
casino slots
casino usa
2017-11-06, 16:09:22
Latisha from Ukraine  
rating
paydayloans
online payday loans
loans for bad credit
online payday loan
2017-11-09, 11:50:01
Clair from Ukraine  
rating
online payday loan
fast payday loans
online loan
payday loans no credit check

 

 

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: