アプリケーションを初期化するコードを作成する(dbExpress チュートリアル)

提供: RAD Studio
移動先: 案内検索

チュートリアル:アプリケーションで dbExpress を使用してデータベースを表示および更新する への移動


アプリケーションを起動するときに、初期化処理を行う必要があります。

宣言の追加

変数をいくつか宣言する必要があります。

Delphi

[コード]タブをクリックします。

uses 句に "DBXDataExpressMetaDataProvider" を追加します。 "iniFiles" および "strUtils" も uses 句に含めておく必要があります。

interface セクションの type の部分に以下の宣言を追加します。

  private
    { Private declarations }
    TableName: String;
    AllTables: TStringList;  //list of database tables
    FMetaDataProvider: TDBXDataExpressMetaDataProvider;
    MemoForm: TFormCurrentField;  //form to view individual elements in database table

これらの変数は後で使用します。 また、TFormCurrentField という型は、後のセクションで宣言します。

interface セクションの末尾に、初期化に必要な定数を次のように追加します。

const
  sDefaultFilterText = 'Filter (* as wild)';

C++

[cdsmain.h]タブをクリックし、次の行を他の #include に追加します。

# include <DBXDataExpressMetaDataProvider.hpp>

次の変数を cdsmain.hprivate の下に追加します。

private:	// ユーザー宣言
  // Private declarations
  String TableName;
  TStringList *AllTables;  //list of database tables
  TDBXDataExpressMetaDataProvider *FMetaDataProvider;
  TFormCurrentField *MemoForm;  //form to view individual elements in database table

これらの変数は後で使用します。 また、TFormCurrentField という型は、後のセクションで宣言します。

初期化に必要な定数を cdsmain.h に追加します。

# define sDefaultFilterText "Filter (* as wild)"

初期化コード

ここで、このサンプル アプリケーションの初期化コードを追加します。 [デザイン]ビューでフォームを選択し、[オブジェクト インスペクタ][イベント]タブで[OnCreate]イベントをダブルクリックします。 すると、イベント ハンドラ スタブが作成され、フォーカスがコード エディタに切り替わります。 次のコードを追加します。

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;
}

コメント

GetConnectionRegistryFile 関数は、[データ エクスプローラ]内のすべての接続に関する情報を取得します。 この String を、TIniFile のコンストラクタに渡す必要があります。 その後、TIniFile オブジェクトの ReadSections プロシージャを使って、すべてのデータベース接続のリストを取得することができます。

実際にコンボ ボックスに追加されるのは、末尾が "CONNECTION" の接続だけです。

前のチュートリアル

メイン ダイアログにコンポーネントを設定する

次のチュートリアル

メイン イベントを処理するコードを作成する