Solved

Create BDE Alias on the fly

Posted on 2002-07-10
4
1,359 Views
Last Modified: 2007-12-19
Dear Experts, please help !

     I am trying to make the BDE-alias on the fly. When I pressed the button for the first
time, the table1 can't open and I got this error message 'Unknown database alias : Temp_Alias',
but when I pressed the button again, the table1 can be opened.      
     Please help me to correct the problem.

Thanks from apin


ps. I'm using D5 Enterprise, Windows ME and paradox table.

{ main program }

unit uCreate_Alias;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids, DBGrids, Db, DBTables ;
type
  TForm1 = class(TForm)
    Open_Table_Btn: TButton;
    Table1: TTable;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    procedure Open_Table_BtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

Uses CreateMyAlias ;

procedure TForm1.Open_Table_BtnClick(Sender: TObject);
begin
  CreateAlias('Temp_Alias','C:\Temp\','STANDARD',True) ;
  Table1.Close ;
  Table1.DatabaseName := 'Temp_Alias' ;
  Table1.TableName    := 'Coba.db' ;
  try
    Table1.Open ;
  except
    On E:Exception Do Showmessage('Error ' + E.Message) ;
  end ; // try
end;

end.



{  CreateMyAlias.pas }

unit CreateMyAlias  ;

interface

uses
  Windows, SysUtils, Messages, Dialogs, DBIPROCS, DBIERRS, DBTables      ;

Const
  CRLF = #13 + #10 ;
  ERR_ALIASDRIVERNOTFOUND = 'Specified drive do not exist ' ;
  ERR_ALIASALREADYEXISTS  = 'The alias (%s) already exists. ' + CRLF +
                            'would you like to reconfigure it ?!' ;
  ERR_ALIASINVALIDPARAM   = 'Invalid alias name' ;
  ERR_ALIASCLOSEBDE       = 'Error closing the BDE ' + CRLF +
                            'Please close all application and restart windows....' ;
  ERR_ALIASOPENBDE        = 'Error initializing BDE, Cannot create alias ' ;

procedure CreateAlias(sAlias, sPath, sDriver : String ; PersistentAlias : Boolean ) ;

implementation

procedure CreateAlias(sAlias, sPath, sDriver : String ; PersistentAlias : Boolean ) ;
ResourceString
  cfg_File = 'C:\Program Files\Common Files\Borland Shared\BDE\Idapi.cfg' ;
var
  dbenv : DbiEnv ;
  dbRes : DbiResult  ;
  Resp  : Word ;
begin
   // Set the BDE  Environment
   With dbEnv Do Begin
      StrPCopy(szWorkDir, sPath ) ;
      StrPCopy(szIniFile,cfg_file) ; // StrPCopy(szIniFile,'')
      bForceLocalInit := True ;
      StrPCopy(szLang,'') ;
      StrPCopy(szClientName,'dbClientName') ;
   End ; // With
   // Initialize BDE with the environment dbEnv
   IF DbiInit(@dbEnv) <> DbiERR_NONE Then
      raise Exception.Create(ERR_ALIASOPENBDE) ;

   // Add the specified alias
   IF sDriver = 'STANDARD' then
        dbRes := DbiAddAlias(NIL,pChar(sAlias),NIL,pChar('PATH:' + sPath),PersistenTAlias)
   Else dbRes := DbiAddAlias(NIL,pChar(sAlias),pChar(sDriver),pChar('PATH:' + sPath),PersistenTAlias) ;

   Case dbRes OF
     DbiERR_INVALIDPARAM : raise Exception.Create(ERR_ALIASINVALIDPARAM) ;
     DbiERR_NAMENOTUNIQUE : begin
        resp := MessageBox(0,PChar(Format(ERR_ALIASALREADYEXISTS,[sAlias])),'CreateAlias',MB_ICONSTOP + MB_YESNO) ;
        IF Resp = ID_YES Then Begin
           Check(DbiDeleteAlias(NIL,pChar(sAlias))) ;
           CreateAlias(sAlias,sPath,sDriver,PersistentAlias) ;
        end ; // if
     end ;
     DbiERR_UNKNOWNDRIVER : raise Exception.Create(ERR_ALIASDRIVERNOTFOUND) ;
   End ; // Case
   IF DbiExit <> DbiERR_NONE then raise Exception.Create(ERR_ALIASCLOSEBDE) ;
   // Else ShowMessage(sAlias + ' successfully created..... ') ;
end ;


end.
0
Comment
Question by:pc_melsa
4 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 200 total points
ID: 7145396
Hello

  here's another way to make alias on the run time, it's easier and working fine with me

uses
  DBIProcs, DBITypes;

procedure AddBDEAlias(sAliasName, sAliasPath, sDBDriver: string);
  var
    h : hDBISes;
begin
  DBIInit(nil);
  DBIStartSession('Temp_Session', h, '');
  DBIAddAlias(nil, PChar(sAliasName), PChar(sDBDriver),
  PChar('PATH:' + sAliasPath), True );
  DBICloseSession(h);
  DBIExit;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  AddBDEAlias('Temp_alias1', 'C:\Temp', 'Paradox');
  Table1.DatabaseName := 'Temp_alias';
  Table1.TableName := 'test';
  Table1.Open;
end;

Best regards
Mohammed Nasman
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7145461
there is also a way using the default-session
and no dbi... routines

just searching in my paqs

meikl ;-)
0
 

Author Comment

by:pc_melsa
ID: 7149684
Thanks for Mr. Mohammad Nasman's comment !
But the procedure AddBDEAlias() provide no error checking.
0
 

Expert Comment

by:CleanupPing
ID: 9343073
pc_melsa:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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