Solved

Open database at runtime

Posted on 2006-07-16
13
318 Views
Last Modified: 2011-09-20
My database is in my app.exe folder (and I want my app. to allways look for it there and display error otherwise).
Also I need to tell my app that the table to open is in that database.How?
What is the code supposed to look like?

0
Comment
Question by:Vrtnar
  • 5
  • 5
  • 3
13 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17119372

What kind of database are you using (pdox, access, dbase, etc)?

Russell

0
 

Author Comment

by:Vrtnar
ID: 17119382
I am using absolute database (*.abs)
0
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17119420
well.. assuming you are using a TDataset descendent such as TTable then you would do

table1.databasename := ExtractFilePath(Paramstr(0)); // same dir as .exe

if you want to do a search in the current dir and find the files yourself, you can do this :-

procedure TForm1.Button1Click(Sender: TObject);
    var
        searchrec: tsearchrec;
        databasefiles: tstringlist;
        searchresult: integer;
    begin
        databasefiles := TStringList.Create;
        try
            searchresult := findfirst(extractfilepath(paramstr(0)) + '*.abs', faAnyFile, SearchRec);
            while searchresult = 0 do
            begin
                if pos('.', SearchRec.Name) <> 1 then
                  databasefiles.Add(SearchRec.Name);
                searchresult := FindNext(SearchRec);
            end;
            memo1.lines.assign(databasefiles); // show the list of databse files in a memo
        finally
            databasefiles.Clear;
            databasefiles.Free;
        end;
    end;
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 17

Expert Comment

by:TheRealLoki
ID: 17119431
if you have 1 database file (file.abs) you can do a simple check like

if FileExists(ExtractFilePath(Paramstr(0)) + 'file.abs') then
  showmessage('file is here, i can continue')
else
  raise exception.create('file.abs does not exist');
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 150 total points
ID: 17119438
Example that uses Absolute database components

Russell

----

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Db, ABSMain;

type
  TForm1            =  class(TForm)
     ABSDatabase1:  TABSDatabase;
     ABSTable1:     TABSTable;
     procedure      FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1:            TForm1;

// Change to your database and table name
const
  DbFile            =  'Demos.abs';
  DbTable           =  'employee';

implementation
{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var  szDbFile:      String;
begin

  szDbFile:=ExcludeTrailingbackslash(ExtractFilePath(Application.ExeName))+'\'+DbFile;
  if FileExists(szDbFile) then
  begin
     ABSDatabase1.DatabaseName:='Demos';
     ABSDatabase1.DatabaseFileName:=szDbFile;
     ABSDatabase1.Open;
     ABSTable1.DatabaseName:='Demos';
     ABSTable1.TableName:=DbTable;
     ABSTable1.Open;
  end
  else
     raise Exception.CreateFmt('Database file "%s" does not exist!', [DbFile]);

end;

end.

--- dfm ---
object Form1: TForm1
  Left = 285
  Top = 114
  Width = 529
  Height = 334
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object ABSDatabase1: TABSDatabase
    CurrentVersion = '5.06 '
    DatabaseName = 'Demos'
    Exclusive = False
    MaxConnections = 500
    MultiUser = False
    SessionName = 'Default'
    Left = 112
    Top = 12
  end
  object ABSTable1: TABSTable
    CurrentVersion = '5.06 '
    InMemory = False
    ReadOnly = False
    Exclusive = False
    Left = 140
    Top = 12
  end
end




0
 

Author Comment

by:Vrtnar
ID: 17119444

procedure TForm1.FormCreate(Sender: TObject);
var
DataBaseName,DatabaseFileName: string;
begin
Path := ExtractFilePath(Application.ExeName);
DatabaseName:= ExtractFilePath(ParamStr(0)) + 'Mydatabase.abs';

should go something like that...?


0
 

Author Comment

by:Vrtnar
ID: 17119468
ExcludeTrailingbackslash is specific to a platform ?!
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17119495
Nope, included in sysutils and my personal coding preference;

szDbFile:=ExcludeTrailingbackslash(ExtractFilePath(Application.ExeName))+'\'+DbFile;

(notice i include the '\' between the path and file name). You can change it to the following if it suits you:

szDbFile:=ExtractFilePath(Application.ExeName)+DbFile;

---
Russell




0
 

Author Comment

by:Vrtnar
ID: 17119501
well libby,i did your suggestion and it worked.what i do not understand is the
szDbFile thing.
0
 
LVL 17

Assisted Solution

by:TheRealLoki
TheRealLoki earned 50 total points
ID: 17119503
procedure TForm1.FormCreate(Sender: TObject);
var
  DataBaseName, DatabaseFileName: string;
begin
  DatabaseName := 'Mydatabase';
  DatabaseFileName:= ExtractFilePath(ParamStr(0)) + 'Mydatabase.abs';
  if not FileExists() then Raise Exception.Create('File does not exist' + #13#10 + DatabaseFileName);

//and as russel points out it would look like

     ABSDatabase1.DatabaseName:= DatabaseName; // 'MyDatabase'
     ABSDatabase1.DatabaseFileName:=DatabaseFileName; // e.g. 'c:\...\Mydatabase.abs';
     ABSDatabase1.Open;
     ABSTable1.DatabaseName:= DatabaseName;  // 'MyDatabase'
     ABSTable1.TableName:= 'Users'; // or some other table in your database
     ABSTable1.Open;
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17119531

The szDbFile "thing" is the Application's path PLUS the database file name which are used to build an absolute file name. The only change required in the example I gave was the 2 constansts: database name and table name.

Russell
0
 

Author Comment

by:Vrtnar
ID: 17119551
Loki :

procedure TForm1.FormCreate(Sender: TObject);
var
  DataBaseName, DatabaseFileName: string;
begin
  DatabaseName := 'Mydatabase';
  DatabaseFileName:= ExtractFilePath(ParamStr(0)) + Mydatabase.abs';
  if not FileExists() then Raise Exception.Create('File does not exist' + #13#10 + DatabaseFileName);
    end else
ABSDatabase1.DatabaseName:= DatabaseName;     ABSDatabase1.DatabaseFileName:=DatabaseFileName;
ABSDatabase1.Open;
     ABSTable1.DatabaseName:= DatabaseName;  
     ABSTable1.TableName:= 'Mytable';
     ABSTable1.Open;
  end;
  I get stopped here : if not FileExists() then
[Error] Unit1.pas(234): Not enough actual parameters
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17119557

if not FileExists(DatabaseFileName) then Raise Exception.Create('File does not exist' + #13#10 + DatabaseFileName);
    end else

0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question