Solved

Create BDE Alias on the fly

Posted on 2002-07-10
4
1,351 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

932 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now