|
<< Click to Display Table of Contents >> Template program for Borland C/C++ |
![]() ![]()
|
This program 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 and gets two records.
Use this template program as a skeleton on which to build your own BDE programs.
#include <idapi.h>
#include <stdio.h>
#include <windows.h>
DBIResult Chk(DBIResult); // Function Prototype
void main ()
{
hDBIDb hDb = 0; // Handle to the Database
hDBICur hCur = 0; // Handle to the cursor (table)
CHAR szTblName[DBIMAXNAMELEN];
CHAR szTblType[DBIMAXNAMELEN];
CURProps curProps; // Properties of the cursor
pBYTE pRecBuf; // Pointer to the record buffer
DFLOAT custNum;
BOOL isBlank;
printf("\nInitialize engine");
Chk(DbiInit(NULL)); // Step 2
printf("\nOpen database");
Chk(DbiOpenDatabase( // Step 3
NULL, // Database name - NULL for standard database
NULL, // Database type - NULL for standard database
dbiREADWRITE, // Open mode - Read/Write or Read only
dbiOPENSHARED, // Share mode - Shared or Exclusive
NULL, // Password - not needed for the STANDARD database
NULL, // Number of optional parameters
NULL, // Field Desc for optional parameters
NULL, // Values for the optional parameters
&hDb)); // Handle to the database
printf("\nSet table directory");
Chk(DbiSetDirectory( // Step 5
hDb, // Handle to the database which is being modified
"e:\\bde32\\examples\\tables"));
// The new working directory
printf("\nSet private directory");
Chk(DbiSetPrivateDir( // Step 6
"c:\\temp")); // Select a directory on a local drive not used
// by other applications.
strcpy(szTblName, "customer");
strcpy(szTblType, szPARADOX);
printf("\nOpen table");
Chk(DbiOpenTable( // Step 7
hDb, // Handle to the standard database
szTblName, // Name of the table
szTblType, // Type of the table - only used for local tables
NULL, // Index Name - Optional
NULL, // IndexTagName - Optional. Only used by dBASE
0, // IndexId - 0 = Primary.
dbiREADWRITE, // Open Mode - Read/Write or Read Only
dbiOPENSHARED, // Shared mode - SHARED or EXCL
xltFIELD, // Translate mode - Almost always xltFIELD
FALSE, // Unidirectional cursor movement.
NULL, // Optional Parameters.
&hCur)); // Handle to the cursor
printf("\nGet cursor properties");
Chk(DbiGetCursorProps( // Step 8
hCur, // Handle to the cursor
&curProps)); // Properties of the cursor (table)
printf("\nAllocate a record buffer");
pRecBuf = (pBYTE) malloc(curProps.iRecBufSize * sizeof(BYTE)); // Step 9
if (pRecBuf == NULL)
{
// If pRecBuf is NULL, there was not enough memory to allocate a
// record buffer. Handling this error will be user determined, but
// no information from the table can be retrieved.
}
else
{
printf("\nSet cursor to the crack before the first record");
Chk(DbiSetToBegin(hCur)); // Step 10
// Position the specified cursor to the crack
// before the first record
printf("\nGet the next record");
Chk(DbiGetNextRecord( // Step 11
hCur, // Cursor from which to get the record.
dbiNOLOCK, // Lock Type
pRecBuf, // Buffer to store the record
NULL)); // Record properties - don't need in this case
printf("\nGet a field out of the record buffer");
Chk(DbiGetField(
hCur, // Cursor which contains the record
1, // Field Number of the "Customer" field.
pRecBuf, // Buffer containing the record
(pBYTE)&custNum, // Variable for the Customer Number
&isBlank)); // Is the field blank?
printf("\nThe retrieved field value is %f", custNum);
}
printf("\nClean-up");
if (pRecBuf != NULL)
free(pRecBuf); // Free the record buffer
if (hCur != 0)
Chk(DbiCloseCursor(&hCur));
// Close the cursor
if (hDb != 0)
Chk(DbiCloseDatabase(&hDb));
// Close the database
DbiExit(); // Close the BDE.
}
DBIResult Chk(DBIResult ErrorValue)
{
char dbi_status[DBIMAXMSGLEN * 5] = {'\0'};
DBIMSG dbi_string = {'\0'};
DBIErrInfo ErrInfo;
if (ErrorValue != DBIERR_NONE)
{
DbiGetErrorInfo(TRUE, &ErrInfo);
if (ErrInfo.iError == ErrorValue)
{
wsprintf(dbi_status, " ERROR %s", ErrInfo.szErrCode);
if (strcmp(ErrInfo.szContext1, ""))
wsprintf(dbi_status, "%s\r\n %s", dbi_status, ErrInfo.szContext1);
if (strcmp(ErrInfo.szContext2, ""))
wsprintf(dbi_status, "%s\r\n %s", dbi_status, ErrInfo.szContext2);
if (strcmp(ErrInfo.szContext3, ""))
wsprintf(dbi_status, "%s\r\n %s", dbi_status, ErrInfo.szContext3);
if (strcmp(ErrInfo.szContext4, ""))
wsprintf(dbi_status, "%s\r\n %s", dbi_status, ErrInfo.szContext4);
}
else
{
DbiGetErrorString(ErrorValue, dbi_string);
wsprintf(dbi_status, " ERROR %s", dbi_string);
}
MessageBox(NULL, dbi_status, "BDE Error", MB_OK | MB_ICONEXCLAMATION);
}
return ErrorValue;
}