Template program for Borland C++ Builder

<< Click to Display Table of Contents >>

Navigation:  Application development > Introduction to BDE programming > BDE Programming in C++Builder >

Template program for Borland C++ Builder

Previous pageReturn to chapter overviewNext page
hmtoggle_plus1See also

Below is the complete program that puts all of the individual steps together. It demonstrates each of the basic steps described in the "Basic procedure" for BDE application development. You can execute the template program and step through it to see how it works: it opens a BDE sample table, visits one record, and retrieves the value from one field in that record. Use this template program as a skeleton on which to build your own BDE programs.

In this example program, the handler for the OnClick event of the TButton component Button1 executes the BDE code. The BDE code accesses a Paradox table and retrieves the value from a field in that table. The retrieved field value is assigned as the value of the Caption property of the TLabel component Label1. Minimal error handling is provided by the Check function.

void __fastcall TForm1::Button1Click(TObject *Sender)

{

  hDBIDb hDb = 0; // Handle to the Database

  hDBICur hCur = 0; // Handle to the cursor (table)

  CHAR szTblName[DBIMAXNAMELEN]; // Table name

  CURProps curProps; // Properties of the cursor

  unsigned char* pRecBuf; // Pointer to the record buffer

  unsigned char* CompanyBuf; // Buffer for the field value

  BOOL isBlank = 0; // Indicates whether retrieved field is blank

 

  Check(DbiInit(NULL));

  Check(DbiOpenDatabase(NULL, NULL, dbiREADONLY, dbiOPENSHARED,

    NULL, NULL, NULL, NULL, hDb));

  Check(DbiSetDirectory(hDb,

    "d:\\Program Files\\Borland\\Borland Shared\\Data"));

  Check(DbiSetPrivateDir("c:\\Temp"));

  strcpy(szTblName, "customer");

  Check(DbiOpenTable(hDb, szTblName, szPARADOX, NULL, NULL, 0,

    dbiREADONLY, dbiOPENSHARED, xltFIELD, FALSE, NULL,                hCur));

  Check(DbiGetCursorProps(hCur, curProps));

  pRecBuf = (unsigned char*) malloc(curProps.iRecBufSize * sizeof(BYTE));

  Check(DbiSetToBegin(hCur));

  Check(DbiGetNextRecord(hCur, dbiNOLOCK, pRecBuf, NULL));

  CompanyBuf = (unsigned char*) malloc(31);

  Check(DbiGetField(hCur, 2, pRecBuf, CompanyBuf, isBlank));

  Label1->Caption = reinterpret_cast<char*>(CompanyBuf);

  if (CompanyBuf != NULL)

    free(CompanyBuf);

  if (pRecBuf != NULL)

    free(pRecBuf);

  if (hCur != 0)

    Check(DbiCloseCursor(hCur));

  if (hDb != 0)

    Check(DbiCloseDatabase(hDb));

  Check(DbiExit());

}