Author: William Gerbert
How to creat a persistent BDE alias
Answer:
There has been a number of occasions where I needed to create persistent BDE
aliases. The point is that the DB API isn't very discussed and is unkown to most
Delphi developers. Despite that fact, the Dbi calls are very powerful and useful
functions.
The function below, CreateAlias, encapsulates the DbiAddAlias call, with some error
checking and BDE initialization and finalization procedures.
1 uses Windows, SysUtils, DbiProcs, DbiErrs, DBTables;
2
3 const
4 CRLF = #13 + #10;
5 ERR_ALIASDRIVERNOTFOUND = 'Specified driver doesn''t exist.';
6 ERR_ALIASALREADYEXISTS = 'The Alias (%s) already exists.' + CRLF +
7 'Would you like to reconfigure it?';
8 ERR_ALIASINVALIDPARAM = 'Invalid Alias name.';
9 ERR_ALIASCLOSEBDE = 'Error closing the BDE.' + CRLF +
10 'Please close all applications and restart Windows';
11 ERR_ALIASOPENBDE = 'Error initializing BDE. Cannot create Alias.';
12
13 procedure CreateAlias(sAlias, sPath, sDriver: string;
14 PersistentAlias: Boolean);
15 var
16 dbEnv: DbiEnv;
17 dbRes: DbiResult;
18 Resp: word;
19 begin
20 { Sets the BDE environment }
21 with dbEnv do
22 begin
23 StrPCopy(szWorkDir, sPath);
24 StrPCopy(szIniFile, '');
25 bForceLocalInit := True;
26 StrPCopy(szLang, '');
27 StrPCopy(szClientName, 'dbClientName');
28 end;
29 { Initalizes BDE with the environment dbEnv }
30 if DbiInit(@dbEnv) <> DbiERR_NONE then
31 raise Exception.Create(ERR_ALIASOPENBDE);
32 { Adds the specified Alias }
33 if sDriver = 'STANDARD' then
34 dbRes := DbiAddAlias(nil, pchar(sAlias), nil,
35 pchar('PATH:' + sPath), PersistentAlias)
36 else
37 dbRes := DbiAddAlias(nil, pchar(sAlias), pchar(sDriver),
38 pchar('PATH:' + sPath), PersistentAlias);
39 case dbRes of
40 DbiERR_INVALIDPARAM:
41 raise Exception.Create(ERR_ALIASINVALIDPARAM);
42 DbiERR_NAMENOTUNIQUE:
43 begin
44 resp := MessageBox(0, pchar(Format(ERR_ALIASALREADYEXISTS, [sAlias])),
45 'CreateAlias', MB_ICONSTOP + MB_YESNO);
46 if Resp = ID_YES then
47 begin
48 Check(DbiDeleteAlias(nil, pchar(sAlias)));
49 CreateAlias(sAlias, sPath, sDriver, PersistentAlias);
50 end;
51 end;
52 DbiERR_UNKNOWNDRIVER:
53 raise Exception.Create(ERR_ALIASDRIVERNOTFOUND);
54 end;
55 if DbiExit <> DbiERR_NONE then
56 raise Exception.Create(ERR_ALIASCLOSEBDE);
57 end; {CreateAlias}
The parameters for this function are:
sAlias: Name of the new alias to be created
sPath: Full path of the directory to which the alias should point. With little
adjustments, this function can be used to create any kind of aliases, and, instead
of passing the path info in this argument, pass all the parameters needed by the
driver to create the alias.
sDriver: Name of an existing BDE driver, such as PARADOX, DBASE, STANDARD
PersistentAlias: Determines whether the new alias will be for future use
(persistent) or just for the actual session.
Example of usage:
58
59 CreateAlias('DBTEST', 'c:\progra~1\borland\delphi~1\projects\cd3\data', 'PARADOX',
60 true);
|