Solved

Create BDE Alias on the fly

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

16 Experts available now in Live!

Get 1:1 Help Now