?
Solved

Create BDE Alias on the fly

Posted on 2002-07-10
4
Medium Priority
?
1,393 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 600 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month15 days, 19 hours left to enroll

850 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