How does one programmatically create a database alias?

By: Christopher Moeller

Abstract: Using AddStandardAlias to create an alias for a session.

Question:  Is it possible to setup and configure aliases programmatically, eliminating the need to use the BDEAdministrator?

Answer:  You may use the Session which is automatically available when using the TDatabase component.

Required components:  Add a TDatabase and a TTable to the form.

Note:  This example uses the method, "AddStandardAlias," for creating aliases (to the session) for Paradox, dBase, or ASCII tables.  If you require an alias for an SQL database server, you must use the AddAlias method instead.
 

//---------------------------------------------------------------------------
    // -Configure session-
    // If alias exists, do not attempt to create one
    if ( !Session->IsAlias(AnsiString("TESTDB")) )
        // Creates an alias until end of session
        Session->AddStandardAlias(
            // Name of alias to create
            AnsiString("TESTDB"),
            // Default path to database, which may be different depending on where you installed Builder
            AnsiString("c:\\Program Files\\Common Files\\Borland Shared\\Data\\"),
            // Type of database
            AnsiString("PARADOX")
            );

    // -Configure database-
    Database1->DatabaseName =  AnsiString("TESTDB");
    Database1->LoginPrompt  =  false;
    Database1->Connected    =  true;

    // -Configure table-
    Table1->DatabaseName =  AnsiString("TESTDB");

    // Indicates which table you would like to use
    Table1->TableName    =  AnsiString("Customer.db");
    Table1->TableType    =  ttParadox;
    Table1->Active       =  true;
//---------------------------------------------------------------------------


Server Response from: ETNASC03