Добавил на всякий случай, чтобы не забыть…. Немного подправил! =)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
// Читаем файл (любой версии) // Проверяем что это ACCESS MDB // Нужны компаненты: // TADOtable,TDataSource,TOpenDialog,TDBGrid,TBitBtn. // Исправил: TUsers на ADOTable1 - Не всем просто сразу понятно, что такое TUsers 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 ADOTable1.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;' + 'Data Source=' + lDBPathName + ';' + 'Persist Security Info=False;' + 'Jet OLEDB:Database Password=' + DBPASSWORD; ADOTable1.TableName := 'Users'; ADOTable1.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. |
Код рабочий, проверял на Delphi 7
Источник: http://www.delphisources.ru/pages/faq/base/read_access_with_ado.html