// BDE - (C) Copyright 1995 by Borland International // Upsize.C #include "snipit.h" static const char szUPTblName[] = "customer"; static const char szUPTblType[] = szPARADOX; static const char szSVTblName[] = "SrvrCustomer"; //===================================================================== // Function: // UpSize(); // // Description: // This example shows how to copy a local table to an SQL server. // This is done using the DbiBatchMove function. // // The example will request that the user supply a server name // and password, and will then proceed to copy the "Customer" // Paradox table (and its primary index) to the table. // // WARNING: // You must have write access to your server in order for this // example to work. //===================================================================== void UpSize (void) { hDBIDb hDb; // Handle to the server database hDBICur hCur; // Server cursor handle hDBIDb hDbPX; // Handle to the Paradox database hDBICur hCurPX; // Paradox cursor handle DBIResult rslt; // Return value of a given function call. UINT16 iLen; // Length of the returned property CURProps TblProps; // Table properties pIDXDesc pIdxDesc = NULL; // Index pointer UINT16 i=0; // Index counter CHAR szIndex[DBIMAXNAMELEN]; // Temporary index name UINT16 iIndexNum = 1; // Index number on the server table BATTblDesc BatTblDesc = { NULL, "", "", { NULL }, { NULL } }; Screen("*** Copying a local table to an SQL Server ***\r\n"); BREAK_IN_DEBUGGER(); Screen(" Initializing IDAPI..."); if (InitAndConnect2(&hDb) != DBIERR_NONE) { Screen("\r\n*** End of Example ***"); return; } // Get the type of the server. rslt = DbiGetProp((hDBIObj)hDb, dbDATABASETYPE, (pCHAR)BatTblDesc.szTblType, sizeof(DBINAME), &iLen); ChkRslt(rslt, "Get Prop"); if (! strcmp(BatTblDesc.szTblType, "STANDARD")) { Screen(" This example only works on remote SQL databases"); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } // Open a standard database for the local table. rslt = DbiOpenDatabase(NULL, NULL, dbiREADWRITE, dbiOPENSHARED, NULL, 0, NULL, NULL, &hDbPX); if (ChkRslt(rslt, "OpenDatabase") != DBIERR_NONE) { CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Setting the database directory for the standard database..."); rslt = DbiSetDirectory(hDbPX, (pCHAR) szTblDirectory); ChkRslt(rslt, "SetDirectory"); Screen(" Open the %s %s table which will be copied to the\r\n" " %s server...", szUPTblName, szUPTblType, BatTblDesc.szTblType); rslt = DbiOpenTable(hDbPX, (pCHAR) szUPTblName, (pCHAR) szUPTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENSHARED, xltFIELD, FALSE, NULL, &hCurPX); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { DbiCloseDatabase(&hDbPX); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } rslt = DbiSetToBegin(hCurPX); ChkRslt(rslt, "SetToBegin"); Screen("\r\n Copying the %s table to the server...", szUPTblName); BatTblDesc.hDb = hDb; strcpy(BatTblDesc.szTblName, (pCHAR)szSVTblName); rslt = DbiBatchMove(NULL, hCurPX, &BatTblDesc, NULL, batCOPY, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, TRUE, TRUE, NULL, TRUE); if(ChkRslt(rslt, "Batch Move") != DBIERR_NONE) { rslt = DbiCloseCursor(&hCurPX); ChkRslt(rslt, "CloseCur"); DbiCloseDatabase(&hDbPX); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } Screen(" Open the %s %s table...", szSVTblName, BatTblDesc.szTblType); rslt = DbiOpenTable(hDb, (pCHAR) szSVTblName, (pCHAR)BatTblDesc.szTblType, NULL, NULL, 0, dbiREADWRITE, dbiOPENEXCL, xltFIELD, FALSE, NULL, &hCur); if (ChkRslt(rslt, "OpenTable") != DBIERR_NONE) { rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName, (pCHAR)BatTblDesc.szTblType); ChkRslt(rslt, "DeleteTable"); rslt = DbiCloseCursor(&hCurPX); ChkRslt(rslt, "CloseCur"); DbiCloseDatabase(&hDbPX); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } // Allocate a buffer for the index descriptors. rslt = DbiGetCursorProps(hCurPX, &TblProps); ChkRslt(rslt, "GetCursorProps"); pIdxDesc = (pIDXDesc)malloc(TblProps.iIndexes * sizeof(IDXDesc)); if ((pIdxDesc == NULL) && (TblProps.iIndexes != 0)) { rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCur"); rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName, (pCHAR)BatTblDesc.szTblType); ChkRslt(rslt, "DeleteTable"); rslt = DbiCloseCursor(&hCurPX); ChkRslt(rslt, "CloseCur"); DbiCloseDatabase(&hDbPX); CloseDbAndExit(&hDb); Screen("\r\n*** End of Example ***"); return; } // Check if there are indexes on the table. if (TblProps.iIndexes != 0) { rslt = DbiGetIndexDescs(hCurPX, pIdxDesc); ChkRslt(rslt, "Get Index Descs"); } Screen(" Add the Unique indexes found in the original table to " "the %s table...", szSVTblName); for ( i = 0; i < TblProps.iIndexes; i++) { // Only add the index if it is unique - such as a Paradox primary // index or certain dBASE indexes. if (pIdxDesc[i].bUnique == TRUE) { // Initialize index descriptor. pIdxDesc[i].bPrimary = FALSE; pIdxDesc[i].bUnique = TRUE; pIdxDesc[i].bDescending = FALSE; pIdxDesc[i].bMaintained = TRUE; pIdxDesc[i].bSubset = FALSE; pIdxDesc[i].bExpIdx = FALSE; pIdxDesc[i].iBlockSize = 0; pIdxDesc[i].iRestrNum = 0; pIdxDesc[i].iIndexId = iIndexNum; strcpy(pIdxDesc[i].szTagName, ""); memset(szIndex, 0, DBIMAXNAMELEN); wsprintf(szIndex, "Index%u",iIndexNum); strcpy(pIdxDesc[i].szName, szIndex); rslt = DbiAddIndex(hDb, hCur, (pCHAR)szSVTblName, (pCHAR)BatTblDesc.szTblType, &pIdxDesc[i], NULL); ChkRslt(rslt, "Add Index"); iIndexNum++; } } memset(&TblProps, 0, sizeof(CURProps)); rslt = DbiGetCursorProps(hCur, &TblProps); ChkRslt(rslt, "GetCursorProps"); rslt = DbiSetToBegin(hCur); ChkRslt(rslt, "SetToBegin"); Screen("\r\n Displaying the first 10 records of the %s %s table...", szSVTblName, BatTblDesc.szTblType); DisplayTable(hCur, 10); Screen("\r\n Close the %s table...", szUPTblName); rslt = DbiCloseCursor(&hCurPX); ChkRslt(rslt, "CloseCur"); Screen(" Close the %s table...", szSVTblName); rslt = DbiCloseCursor(&hCur); ChkRslt(rslt, "CloseCur"); Screen(" Delete the %s table...", szSVTblName); rslt = DbiDeleteTable(hDb,(pCHAR) szSVTblName, (pCHAR)BatTblDesc.szTblType); ChkRslt(rslt, "DeleteTable"); Screen(" Close the databases and exit IDAPI..."); DbiCloseDatabase(&hDbPX); CloseDbAndExit(&hDb); free(pIdxDesc); Screen("\r\n*** End of Example ***"); }