home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bde / snipit.pak / TEXTEXP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  4.2 KB  |  116 lines

  1. // BDE - (C) Copyright 1995 by Borland International
  2.  
  3. // TextExp.c
  4. #include "snipit.h"
  5.  
  6. static const char szSrcTblName[] = "CUSTOMER.DB";
  7. static const char szSrcTblType[] = szPARADOX;
  8.  
  9. static const char szDestTblName[] = "CUSTOMER.TXT";
  10. static const char szDestTblType[] = szASCII;
  11.  
  12. //=====================================================================
  13. //  Function:
  14. //          TextExport();
  15. //
  16. //  Description:
  17. //          This example shows exporting data from a Paradox table
  18. //          to a text file. The "customer.db" table will be used as the
  19. //          source table and a "customer.txt" table will be created as
  20. //          the destination table. The DbiBatchMove function is used to
  21. //          copy the records from one table to another.
  22. //=====================================================================
  23. void
  24. TextExport (void)
  25. {
  26.     DBIResult       rslt;               // Return value from IDAPI functions
  27.     hDBIDb          hDb;                // Handle to the database
  28.     hDBICur         PDhCur;             // Paradox cursor handle
  29.     hDBICur         TXThCur;            // Text cursor handle
  30.     BATTblDesc      TxtTblDesc;         // Descriptor for the text table
  31.  
  32.     Screen("*** Text Export Example ***\r\n");
  33.  
  34.     BREAK_IN_DEBUGGER();
  35.  
  36.     Screen("    Initializing IDAPI...");
  37.     if (InitAndConnect(&hDb) != DBIERR_NONE)
  38.     {
  39.         Screen("\r\n*** End of Example ***");
  40.         return;
  41.     }
  42.  
  43.     Screen("    Setting the database directory...");
  44.     rslt = DbiSetDirectory(hDb, (pCHAR) szTblDirectory);
  45.     ChkRslt(rslt, "SetDirectory");
  46.  
  47.     Screen("    Force the use of a schema file for the text table...");
  48.     rslt = DbiSetProp(hDb, dbUSESCHEMAFILE, (UINT32)TRUE);
  49.     ChkRslt(rslt, "SetProp");
  50.  
  51.     Screen("\r\n    Opening the %s Paradox table...", szSrcTblName);
  52.     rslt = DbiOpenTable(hDb, (pCHAR) szSrcTblName, (pCHAR) szSrcTblType,
  53.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  54.                         xltFIELD, FALSE, NULL, &PDhCur);
  55.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  56.     {
  57.         CloseDbAndExit(&hDb);
  58.         Screen("\r\n*** End of Example ***");
  59.         return;
  60.     }
  61.  
  62.     rslt = DbiSetToBegin(PDhCur);
  63.     ChkRslt(rslt, "SetToBegin");
  64.  
  65.     // Set the information for the destination table.
  66.     TxtTblDesc.hDb = hDb;
  67.     strcpy(TxtTblDesc.szTblName, szDestTblName);
  68.     strcpy(TxtTblDesc.szTblType, szDestTblType);
  69.  
  70.     Screen("\r\n    Create a text table copied from the %s %s table...",
  71.            szSrcTblName, szSrcTblType);
  72.     rslt = DbiBatchMove(NULL, PDhCur, &TxtTblDesc, NULL, batCOPY,
  73.                         0, NULL, NULL, NULL, 0, NULL, NULL,
  74.                         NULL, NULL, NULL, NULL,
  75.                         FALSE, FALSE, NULL, TRUE);
  76.     ChkRslt(rslt, "Batch Move");
  77.  
  78.     // Note that the table type has to be specified with the delimiter
  79.     //   and separator since we are opening the table as a delimited text
  80.     //   file.
  81.     Screen("\r\n    Opening the %s Text table using schema information...\r\n",
  82.            szDestTblName);
  83.     rslt = DbiOpenTable(hDb, (pCHAR)szDestTblName, "ASCIIDRV",
  84.                         NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED,
  85.                         xltFIELD, FALSE, NULL, &TXThCur);
  86.     if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE)
  87.     {
  88.         rslt = DbiCloseCursor(&PDhCur);
  89.         ChkRslt(rslt, "CloseCursor");
  90.         CloseDbAndExit(&hDb);
  91.         Screen("\r\n*** End of Example ***");
  92.         return;
  93.     }
  94.  
  95.     Screen("    Displaying the first 10 records of the %s text table...",
  96.            szDestTblName);
  97.     DisplayTable(TXThCur, 10);
  98.  
  99.     Screen("\r\n    Closing the %s table...", szDestTblName);
  100.     rslt = DbiCloseCursor(&TXThCur);
  101.     ChkRslt(rslt, "CloseCur");
  102.  
  103.     Screen("    Delete the %s table...", szDestTblName);
  104.     rslt = DbiDeleteTable(hDb, (pCHAR)szDestTblName, NULL);
  105.     ChkRslt(rslt, "DeleteTable");
  106.  
  107.     Screen("\r\n    Closing the %s table...", szSrcTblName);
  108.     rslt = DbiCloseCursor(&PDhCur);
  109.     ChkRslt(rslt, "CloseCur");
  110.  
  111.     Screen("    Close the database and exit IDAPI...");
  112.     CloseDbAndExit(&hDb);
  113.  
  114.     Screen("\r\n*** End of Example ***");
  115. }
  116.