Write Code to Initialize the Application (dbExpress Tutorial)
Go Up to Tutorial: Using dbExpress to View and Update Databases in an Application
When the application launches, we need to perform some initialization.
Contents |
Extra Declarations
You need to declare some variables.
Delphi
Click the Code tab.
Add "DBXDataExpressMetaDataProvider"
to the uses clause. Make sure that "System.IniFiles" and "System.StrUtils" are also in the uses clause.
Add the following declarations to the type part of the interface section:
private { Private declarations } TableName: String; AllTables: TStringList; //list of database tables FMetaDataProvider: TDBXDataExpressMetaDataProvider; MemoForm: TFormCurrentField; //form to view individual elements in database table
We will use these variables later. In particular, TFormCurrentField is a type we will declare in a later section.
Add a constant needed for initialization at the end of the interface section:
const sDefaultFilterText = 'Filter (* as wild)';
C++
Click the cdsmain.h tab and add the following to the #include list:
#include <DBXDataExpressMetaDataProvider.hpp>Add these variables under private in cdsmain.h:
private: // User declarations // Private declarations String TableName; TStringList *AllTables; //list of database tables TDBXDataExpressMetaDataProvider *FMetaDataProvider; TFormCurrentField *MemoForm; //form to view individual elements in database table
We will use these variables later. In particular, TFormCurrentField is a type we will declare in a later section.
Add a constant needed for initialization to cdsmain.h:
#define sDefaultFilterText "Filter (* as wild)"Initialization Code
Now add the initialization code for this example application. In the Design view, select the form, then double-click the OnCreate event in the Object Inspector's Event tab. This creates the event handler stub and changes focus to the Code Editor. We add the following code:
Delphi
procedure TForm2.FormCreate(Sender: TObject); var IniFile: TIniFile; SectionsList: TStringList; //for list of connections TempStr: String; I: Integer; begin EditFilter.Text := sDefaultFilterText; // Get information on current database connections IniFile := TIniFile.Create(GetConnectionRegistryFile); // Get a list of the database connections available SectionsList := TStringList.Create; IniFile.ReadSections(SectionsList); // Add the connections to the combo box. // Each valid connection should end with the characters 'CONNECTION', // and only these are added. SectionsList.Sort; for I := 0 to SectionsList.Count - 1 do begin TempStr := UpperCase(AnsiRightStr(SectionsList[I], Length('connection'))); if TempStr = 'CONNECTION' then begin ComboBoxConnections.Items.Add(SectionsList[I]); end; end; // Free objects no longer needed. FreeAndNil(IniFile); FreeAndNil(SectionsList); end;
C++
void __fastcall TForm1::FormCreate(TObject *Sender) { TIniFile *IniFile; TStringList *SectionsList; //for list of connections String TempStr; int I; // Initialize objects AllTables = NULL; FMetaDataProvider = NULL; MemoForm = NULL; EditFilter->Text = sDefaultFilterText; // Get information on current database connections IniFile = new TIniFile(GetConnectionRegistryFile()); // Get a list of the database connections available SectionsList = new TStringList(); IniFile->ReadSections(SectionsList); // Add the connections to the combo box. // Each valid connection should end with the characters 'CONNECTION', // and only these are added. SectionsList->Sort(); for (I = 0; I < SectionsList->Count; I++) { TempStr = UpperCase(AnsiRightStr(SectionsList->Strings[I], strlen("connection"))); if (TempStr == "CONNECTION") { ComboBoxConnections->Items->Add(SectionsList->Strings[I]); } } // Free objects no longer needed. delete IniFile; IniFile = NULL; delete SectionsList; SectionsList = NULL; }
Comments
The GetConnectionRegistryFile function gets information on all connections in the Data Explorer. This String is needed for the TIniFile constructor. We can then use the TIniFile object's ReadSections procedure to get a list of all database connections.
Only connections that end in "CONNECTION" are actually added to the combo box.
Previous
Populate Main Dialog with Components