Como criar Aliases runtime para o BDE

02/03/2003

0

Duas rotinas que demonstram como criar ´programaticamente´ alias para o BDE.

How to create programmatically database alias just for current execution? Answer:

Article 903 <http://www.delphi3000.com/article.asp?id=903> (see also my comments there) shows how to use directory reference instead of database alias.

However you may prefer to use a temporary database alias just for the current execution (to be removed after the application terminates). The following example shows how to create such a temporary alias:

---------------
const
DatabaseAlias = ´MyTempAlias´;
{...}
with Session do begin
ConfigMode := cmSession;
try
AddStandardAlias (DatabaseAlias,
ExtractFilePath(ParamStr(0)) + ´DB´,
´PARADOX´);
finally
ConfigMode := cmAll;
end;
end;
---------------

This piece of code creates temporary database alias named ´MyTempAlias´ for Paradox database located in subdirectory \\DB of the program executable directory. You should perform this code before activating your data controls - say, upon creating your datamodule or any other form with data controls. Note that you may have Database property of your data controls preset for the value ´MyTempAlias´ at design time, so all you have to do to start working with your TTable etc. is to set its Active property to TRUE after executing the code above.

Question/Problem/Abstract:

Search and Create a new Alias with BDE calls.
Answer:


To create a temporary alias has been topic of other article similar ( http://www.delphi3000.com/article.asp?id=913 ) of Dmitri Papichev, but the difference of the previous, that here I make direct calls to the BDE and I seek an equal alias before creating one new, therefore maybe interest to you.

1. Declare BDE, Dbtables in USES section.
2. This set of sentences can be put on the part of initialization of the code of the form or maybe of a DataModule.


Var
AliasFound: Boolean;
TmpCursor: hDBICur;
Rslt: DBIResult;
Database: DBDesc;

Begin
Check(DbiInit(nil));
try
Check(DbiOpenDatabaseList(TmpCursor));
AliasFound := False;
repeat
{Get a DBDesc record for the next alias}
rslt:= DbiGetNextRecord(TmpCursor, dbiNOLOCK, @Database, nil);
if (rslt <> DBIERR_EOF) then
if StrPas(Database.szName) = ´MyAlias´ then
begin
{The alias MyAlias already exists}
AliasFound := True;
Break
end;
until rslt <> DBIERR_NONE;
Check(DbiCloseCursor(TmpCursor));
if not AliasFound then
{If the alias was not found, add it to IDAPI.CFG}
Check(DbiAddAlias(nil,PChar(´MyAlias´),nil,
PChar(´PATH:´+ExtractFilePath(Application.ExeName)),True))
finally
DbiExit;
end;
end;


Carnette

Carnette

Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar