was ist neu ¦  programmier tips ¦  indy artikel ¦  intraweb artikel ¦  informationen ¦  links ¦  interviews
 sonstiges ¦  tutorials ¦  Add&Win Gewinnspiel

Tips (1541)

Dateien (137)
Datenbanken (90)
Drucken (35)
Grafik (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Mathematik (76)
Multimedia (45)
Oberfläche (107)
Objekte/
ActiveX (51)

OpenTools API (3)
Sonstiges (126)
Strings (83)
System (266)
VCL (242)

Tips sortiert nach
Komponente


Tip suchen

Tip hinzufügen

Add&Win Gewinnspiel

Werbung

58 Visitors Online


 
...auf eine Access Datenbank mit ADO zugreifen?
Autor: Michael Casse
[ Tip ausdrucken ]  

Tip Bewertung (10):  
     


// Read an MS-ACCESS Database (any versions) using ADO
// Verify if it is an ACCESS MDB
// Components Needed on the Application Form are:
// TADOtable,TDataSource,TOpenDialog,TDBGrid,TBitBtn.
// Date : 14/01/2002
// Author: Michael Casse.

unit uMain;

interface

uses
  
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, DBTables, ADODB, Grids, DBGrids, ExtCtrls, DBCtrls, StdCtrls, Buttons;

type
  
TfrmMain = class(TForm)
    DSUsers: TDataSource;
    DBGridUsers: TDBGrid;
    BitBtn1: TBitBtn;
    OpenDialog1: TOpenDialog;
    TUsers: TADOTable;
    procedure FormCreate(Sender: TObject);
    procedure ValidateAccessDB;
    function CheckIfAccessDB(lDBPathName: string): boolean;
  private
    
{ Private declarations }
  
public
    
{ Public declarations }
  
end;

var
  
frmMain: TfrmMain;
const
  
DBNAME = 'ADODemo.MDB';
  DBPASSWORD = '123'; // Access DB Password Protected

implementation

{$R *.DFM}

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  
validateAccessDB;
end;

procedure TfrmMain.ValidateAccessDB;
var
  
lDBpathName : String;
  lDBcheck : boolean;
begin
  if 
FileExists(ExtractFileDir(Application.ExeName) + '\' + DBNAME) then
    
lDBPathName := ExtractFileDir(Application.ExeName) + '\' + DBNAME
  else if OpenDialog1.Execute then
    
// Set the OpenDialog Filter for ADOdemo.mdb only
    
lDBPathName := OpenDialog1.FileName;

  lDBCheck := False;
  if Trim(lDBPathName) <> '' then
    
lDBCheck := CheckIfAccessDB(lDBPathName);

  if lDBCheck = True then
  begin
    
// ADO Connection String to the MS-ACCESS DB
    
TUsers.ConnectionString :=
      'Provider=Microsoft.Jet.OLEDB.4.0;' +
      'Data Source=' + lDBPathName + ';' +
      'Persist Security Info=False;' +
      'Jet OLEDB:Database Password=' + DBPASSWORD;
    TUsers.TableName := 'Users';
    TUsers.Active := True;
  end
  else
    
frmMain.Free;
end;

// Check if it is a valid ACCESS DB File Before opening it.

function TfrmMain.CheckIfAccessDB(lDBPathName: string): Boolean;
var
  
UnTypedFile: file of byte;
  Buffer: array[0..19] of byte;
  NumRecsRead: Integer;
  i: Integer;
  MyString: string;
begin
  
AssignFile(UnTypedFile, lDBPathName);
  reset(UnTypedFile);
  BlockRead(UnTypedFile, Buffer, High(Buffer), NumRecsRead);
  CloseFile(UnTypedFile);
  for i := 1 to High(Buffer) do
    
MyString := MyString + Trim(Chr(Ord(Buffer[i])));
  Result := False;
  if Mystring = 'StandardJetDB' then
    
Result := True;
  if Result = False then
    
MessageDlg('Invalid Access Database', mtInformation, [mbOK], 0);
end;

end.

 

Bewerten Sie diesen Tipp:

dürftig
ausgezeichnet


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners