![]() |
![]() |
|||||||||
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|||||
Q: Add, delete, and modify BDE aliases from codeAnswerThe VCL TSession class provides methods for adding, deleting, and modifying BDE aliases. The functions are AddAlias, AddStandardAlias, DeleteAlias, and ModifyAlias. The code example below demonstrates how to use each one. //----------------------------------------------------------------- // ModifyAlias void __fastcall TForm1::btnModifyClick(TObject *Sender) { std::auto_ptr< TStringList > params( new TStringList); params->Values["ENABLE BCD"] = "TRUE"; Session->ModifyAlias("BCDEMOS", params.get()); Session->SaveConfigFile(); } //----------------------------------------------------------------- // AddStandardAlias void __fastcall TForm1::btnAddStandardClick(TObject *Sender) { const AnsiString path = "C:\\Program Files\\Common Files\\Borland Shared\\Data"; Session->AddStandardAlias("BCBDEV_DB", path, "PARADOX"); Session->SaveConfigFile(); } //----------------------------------------------------------------- // AddAlias void __fastcall TForm1::btnAddClick(TObject *Sender) { std::auto_ptr< TStringList > params( new TStringList); params->Values["ENABLE BCD"] = "TRUE"; params->Values["DATABASE NAME"] = "production"; params->Values["SERVER NAME"] = "NTS_PROD"; params->Values["USER NAME"] = "hhowe"; params->Values["PASSWORD"] = "mfcblowschunks"; Session->AddAlias("BCBDEV_SQL", "MSSQL", params.get()); Session->SaveConfigFile(); } //----------------------------------------------------------------- // DeleteAlias void __fastcall TForm1::btnDeleteClick(TObject *Sender) { Session->DeleteAlias("BCBDEV_DB"); Session->SaveConfigFile(); } //-----------------------------------------------------------------
ModifyAliasThe first OnClick handler uses ModifyAlias to alter the BCDEMOS alias that comes with C++Builder. ModifyAlias takes two parameteters. The first is an AnsiString 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. The stringlist should be formatted like this: Property=Value 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: ENABLE BCD=TRUE Case sensitivy does not matter. The easiest way to format a TStringList this way is to use the Values array property. The Values 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 Params property of TDatabase. One nice feature of the Values property is that it is smart enough to know whether it should add a string to the list, or just modify an existing line. After we call ModifyAlias, there is still more work to be done. ModifyAlias 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 SaveConfigFile method of TSession. Notice that each OnClick handler calls SaveConfigFile. AddStandardAliasThe AddStandardAlias member of TSession allows you to add a Paradox, dBase, or ASCII alias. AddStandard 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.
AddStandardAlias does not allow you to pass a stringlist of paramaters. Call the ModifyAlias if you want to modify a standard alias after creating it. AddAliasAddAlias 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. DeleteAliasDeleteAlias is the simplest of the four functions to understand. You simply pass it the name of the alias to delete. After calling DeleteAlias, you must call the SaveConfigFile method in order to make the changes permanent. NotesThese member functions of TSession are simply wrappers for the BDE api. The code for the FAQ could be rewritten to use the BDE directly without even messing with TSession.
| ||||||||||
All rights reserved. |