📄 faq88.htm
字号:
<HTML>
<HEAD>
<TITLE>Add, delete, and modify BDE aliases from code</TITLE>
<META NAME="Author" CONTENT="Harold Howe">
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="640">
<TR>
<TD>
<H3> Add, delete, and modify BDE aliases from code </H3>
<P> The VCL <TT>TSession</TT> class provides methods for adding, deleting, and
modifying BDE aliases. The functions are <TT>AddAlias</TT>, <TT>AddStandardAlias</TT>,
<TT>DeleteAlias</TT>, and <TT>ModifyAlias</TT>. The code example below demonstrates how to
use each one.
</p>
<pre>
<font color="navy">//-----------------------------------------------------------------</font>
<font color="navy">// ModifyAlias</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>btnModifyClick<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
std<b>:</b><b>:</b>auto_ptr< TStringList <b>></b> params<b>(</b> <b>new</b> TStringList<b>)</b><b>;</b>
params<b>-></b>Values<b>[</b><font color="blue">"ENABLE BCD"</font><b>]</b> <b>=</b> <font color="blue">"TRUE"</font><b>;</b>
Session<b>-></b>ModifyAlias<b>(</b><font color="blue">"BCDEMOS"</font><b>,</b> params<b>.</b>get<b>(</b><b>)</b><b>)</b><b>;</b>
Session<b>-></b>SaveConfigFile<b>(</b><b>)</b><b>;</b>
<b>}</b>
<font color="navy">//-----------------------------------------------------------------</font>
<font color="navy">// AddStandardAlias</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>btnAddStandardClick<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
<b>const</b> AnsiString path <b>=</b>
<font color="blue">"C:\\Program Files\\Common Files\\Borland Shared\\Data"</font><b>;</b>
Session<b>-></b>AddStandardAlias<b>(</b><font color="blue">"BCBDEV_DB"</font><b>,</b> path<b>,</b> <font color="blue">"PARADOX"</font><b>)</b><b>;</b>
Session<b>-></b>SaveConfigFile<b>(</b><b>)</b><b>;</b>
<b>}</b>
<font color="navy">//-----------------------------------------------------------------</font>
<font color="navy">// AddAlias</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>btnAddClick<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
std<b>:</b><b>:</b>auto_ptr< TStringList <b>></b> params<b>(</b> <b>new</b> TStringList<b>)</b><b>;</b>
params<b>-></b>Values<b>[</b><font color="blue">"ENABLE BCD"</font><b>]</b> <b>=</b> <font color="blue">"TRUE"</font><b>;</b>
params<b>-></b>Values<b>[</b><font color="blue">"DATABASE NAME"</font><b>]</b> <b>=</b> <font color="blue">"production"</font><b>;</b>
params<b>-></b>Values<b>[</b><font color="blue">"SERVER NAME"</font><b>]</b> <b>=</b> <font color="blue">"NTS_PROD"</font><b>;</b>
params<b>-></b>Values<b>[</b><font color="blue">"USER NAME"</font><b>]</b> <b>=</b> <font color="blue">"hhowe"</font><b>;</b>
params<b>-></b>Values<b>[</b><font color="blue">"PASSWORD"</font><b>]</b> <b>=</b> <font color="blue">"mfcblowschunks"</font><b>;</b>
Session<b>-></b>AddAlias<b>(</b><font color="blue">"BCBDEV_SQL"</font><b>,</b> <font color="blue">"MSSQL"</font><b>,</b> params<b>.</b>get<b>(</b><b>)</b><b>)</b><b>;</b>
Session<b>-></b>SaveConfigFile<b>(</b><b>)</b><b>;</b>
<b>}</b>
<font color="navy">//-----------------------------------------------------------------</font>
<font color="navy">// DeleteAlias</font>
<b>void</b> <b>__fastcall</b> TForm1<b>:</b><b>:</b>btnDeleteClick<b>(</b>TObject <b>*</b>Sender<b>)</b>
<b>{</b>
Session<b>-></b>DeleteAlias<b>(</b><font color="blue">"BCBDEV_DB"</font><b>)</b><b>;</b>
Session<b>-></b>SaveConfigFile<b>(</b><b>)</b><b>;</b>
<b>}</b>
<font color="navy">//-----------------------------------------------------------------</font>
</pre>
<P>
<H4>
ModifyAlias
</H4>
<P>
The first <TT>OnClick</TT> handler uses <TT>ModifyAlias</TT> to alter the <TT>BCDEMOS</TT> alias that comes with
C++Builder. <TT>ModifyAlias</TT> takes two parameteters. The first is an <TT>AnsiString</TT> that contains the
name of the alias that you want to modify. The second argument is a stringlist that contains the alias parameters that
you want to change. You don't have to list all of the available parameters for a given alias. You only have to list
parameters that you want to change. Any parameter that you omit will remain unchanged.
</P>
<P>
The stringlist should be formatted like this:
</P>
<PRE>
Property=Value
</PRE>
<P>
For example, the example code sets the "Enable BCD" property of the alias to true. In order to set this property, the
stringlist should contain this:
</p>
<PRE>
ENABLE BCD=TRUE
</PRE>
<P>
Case sensitivy does not matter. The easiest way to format a <TT>TStringList</TT> this way is to use the <TT>Values</TT>
array property. The <TT>Values</TT> property allows you to treat the stringlist as an assortment of properties and their
values. It's great for reading config files or INI files. It also makes it easy to work with the <TT>Params</TT>
property of <TT>TDatabase</TT>. One nice feature of the <TT>Values</TT> property is that it is smart enough to know
whether it should add a string to the list, or just modify an existing line.
</P>
<P>
After we call <TT>ModifyAlias</TT>, there is still more work to be done. <TT>ModifyAlias</TT> only changes the BDE
settings for the current BDE session. The changes are not saved permanently to the alias. In order to save the changes
permanently, we need to call the <TT>SaveConfigFile</TT> method of <TT>TSession</TT>. Notice that each <TT>OnClick</TT>
handler calls <TT>SaveConfigFile</TT>.
</P>
<H4>
AddStandardAlias
</H4>
<P>
The <TT>AddStandardAlias</TT> member of <TT>TSession</TT> allows you to add a Paradox, dBase, or ASCII alias.
<TT>AddStandard</TT> takes three arguments. The first is the name of the alias to create. This string must be unique.
The second parameter is the path to the database. The example code sets the directory to the same directory where the
BCB sample database resides. The last argument determines which driver the alias will use: Paradox, dBase, or ASCII. The
thrird argument should contain one of these strintgs.
</P>
<UL>
<LI> PARADOX
<LI> DBASE
<LI> ASCIIDRV
</UL>
<P>
<TT>AddStandardAlias</TT> does not allow you to pass a stringlist of paramaters. Call the <TT>ModifyAlias</TT> if you want to
modify a standard alias after creating it.
</p>
<H4>
AddAlias
</H4>
<P>
<TT>AddAlias</tt> adds a new SQL links alias to the BDE. You pass it the name of the alias, the driver to use, and a
stringlist that contains the alias parameters. The code example creates a new alias called BCBDEV_SQL. The alias uses
SQL links driver for MS Sql Server.
</P>
<H4>
DeleteAlias
</H4>
<P>
<TT>DeleteAlias</TT> is the simplest of the four functions to understand. You simply pass it the name of the alias to
delete. After calling <TT>DeleteAlias</TT>, you must call the <TT>SaveConfigFile</TT> method in order to make the
changes permanent.
</P>
<H4>
Notes
</H4>
<P>
These member functions of <TT>TSession</TT> are simply wrappers for the BDE api. The code for the FAQ could be
rewritten to use the BDE directly without even messing with <TT>TSession</TT>.
</P>
<BR>
<TABLE BORDER=1 CELLPADDING=10 CELLSPACING=0 WIDTH="100%">
<TR> <TD colspan = 2><B>Downloads for this FAQ</B> </TD> </TR>
<TR> <TD><TT><A HREF="download/faq88.zip" >faq88.zip </A></TT></TD><TD>Source code for this FAQ.</TD> </TR>
</TABLE>
</TD> </TR>
</TABLE>
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -