Mega Code Archive

 
Categories / Delphi / ADO Database
 

Bde fonksiyonları 15

Set the range for the specified cursor and return the amount of records in the range. Delphi programs should call the SetRange, ApplyRange, ResetRange methods of a TTable. This example sets the range for the specified cursor and returns the amount of records in the range. For this example to operate, the first field of the table must be numeric. such as, STOCK.DB. This example uses the following input: fDbiSetRange(Table1.Handle, Count); The procedure is: procedure fDbiSetRange(hTmpCur: hDBICur; var Count: LongInt); var pMinBuf, pMaxBuf: PByte; key_min, key_max: double; CurProp: CURProps; begin pMinBuf := nil; pMaxBuf := nil; key_min := 1000.00; key_max := 2000.00; Check(DbiGetCursorProps(hTmpCur, CurProp)); GetMem(pMinBuf,CurProp.iRecBufSize); if (pMinBuf = nil) then Check(DBIERR_NOMEMORY); GetMem(pMaxBuf,CurProp.iRecBufSize); if (pMaxBuf = nil) then Check(DBIERR_NOMEMORY); try Check(DbiPutField(hTmpCur, 1, pMinBuf, @key_min)); Check(DbiPutField(hTmpCur, 1, pMaxBuf, @key_max)); Check(DbiSetRange(hTmpCur, False, 0, 0, pMinBuf, False, 0, 0, pMaxBuf, True)); // Set the return count for the number of records in the limited range Count := 0; Check(DbiGetRecordCount(hTmpCur, Count)); Check(DbiResetRange(hTmpCur)); finally FreeMem(pMinBuf); FreeMem(pMaxBuf); end; end; //********************************************************************************* Position the cursor at the beginning of the table. Delphi programs should use the First method from a TDataset object. This method positions the cursor at the beginning of the result set. This example uses the following input: fDbiSetToBegin(hCur); The procedure is: procedure fDbiSetToBegin(hTmpCur: hDbiCur); begin Check(DbiSetToBegin(hTmpCur)); end; //********************************************************************************** Set the position of the destination cursor to the position of the source cursor Delphi users should use the GoToCurrent method associated the TTable component rather than directly calling DbiSetToCursor. This method is defined as: procedure GoToCurrent(Table: TTable); The following is an example: Table1.GotoCurrent(Table2); //*********************************************************************************** Position the cursor at the end of the result set. Delphi programs should use the Last method from a TDataset object. This example uses the following input: fDbiSetToEnd(hCur); The procedure is: procedure fDbiSetToEnd(hTmpCur: hDbiCur); begin Check(DbiSetToEnd(hTmpCur)); end; //*********************************************************************************** Find the specified value in the table and return the record and record buffer This example works with the STOCK.DB table open on the primary key and uses the following input: fDbiSetToKey(hCur, pRecBuf); The function is: function fDbiSetToKey(hTmpCur: hDBICur; pTmpRecBuf: PByte): Longint; var key: Double; RecProp: RecProps; begin key:= 1330.00; Check(DbiInitRecord(hTmpCur, pTmpRecBuf)); Check(DbiPutField(hTmpCur, 1, pTmpRecBuf, @key)); Check(DbiSetToKey(hTmpCur, keySEARCHEQ, False, 0, 0, pTmpRecBuf)); Check(DbiGetNextRecord(hTmpCur, dbiNoLock, pTmpRecBuf, @RecProp)); Result := RecProp.iSeqNum; end; //************************************************************************************** Position the cursor to the given physical record number. Valid only for dBASE and FoxPro tables. The function fDbiSetToRecordNo, below, positions the cursor in the TTable specified in the Tbl parameter to the record number specified in RecordNum. Call the TTable component’s Resync method after repositioning the record pointer with DbiSetToRecordNo to synchronize the TTable with the underlying dataset. This example uses the following input: fDbiSetToRecordNum(DBASEANIMALS, 20); The procedure is: procedure fDbiSetToRecordNo(Tbl: TTable; RecordNum: LongInt); var rslt: dbiResult; begin rslt:= DbiSetToRecordNo(Tbl.handle, RecordNum); if (rslt <> DBIERR_NONE) then begin if (rslt = DBIERR_EOF) then tbl.last; if (rslt = DBIERR_BOF) then tbl.first; end; Tbl.Resync([]); end; //************************************************************************************** Position the cursor to the specified sequence number of the table. If the table is not a Paradox type, the cursor is not moved. Call the TTable component’s Resync method after repositioning the record pointer with DbiSetToRecordNo to synchronize the TTable with the underlying dataset. This example uses the following input: fDbiSetToSeqNo(Table1, 40); The procedure is: procedure fDbiSetToSeqNo(var Tbl: TTable; RecNum: Longint); var Props: CurProps; begin Check(DbiGetCursorProps(Tbl.Handle, Props)); if (StrComp(Props.szTableType, szPARADOX) = 0) then Check(DbiSetToSeqNo(Tbl.Handle, RecNum)); Tbl.Resync([]); end; //************************************************************************************** Sort the source table into the destination table on the given field. This example uses the following input: fDbiSortTable(CustomerTbl, CustomerTbl2, CustomerTbl.FieldByName('COMPANY')); The function is: function fDbiSortTable(SrcTbl, DestTbl: TTable; SortField: TField): Longint; var Field: Word; CaseIns: Boolean; Recs: Longint; begin Recs := SrcTbl.RecordCount; CaseIns := True; Field := SortField.Index + 1; if not DestTbl.Active then raise EDatabaseError.Create('Cannot complete operation with ' + 'destination table closed'); Check(DbiSortTable(SrcTbl.DBHandle, nil, nil, SrcTbl.Handle, nil, nil, DestTbl.Handle, 1, @Field, @CaseIns, nil, nil, False, nil, Recs)); Result := Recs; end; //************************************************************************************** Example 1: Start a new session for the client application. Delphi programs can use the TSession object in the component library. procedure fDbiStartSession(pName: string; var hSes: hDBISes; pNetDir: string); begin Check(DbiStartSession(PChar(pName), hSes, PChar(pNetDir))); end; Example 2: Create a new session and return the session number. Most Delphi users can use TSession.Open, TSessionList.OpenSession or the TSession component. This example uses the following input: SesNo := fDbiStartSession('NewSession', hSes, 'C:\Netdir'); The function is: function fDbiStartSession(pName: string; var hSes: hDBISes; pNetDir: string): Word; var Ses: SESInfo; begin Check(DbiStartSession(PChar(pName), hSes, PChar(pNetDir))); Check(DbiGetSesInfo(Ses)); Result := Ses.iSession; end; //************************************************************************************* Set cursor to the specified index name: Users of TTable objects should use the IndexName property to change indexes. Set cursor to the specified index name and keep the cursor on the same record. This example uses the following input: fDbiSwitchToIndex(Table1.Handle, 'VendorNo'); The procedure is: procedure fDbiSwitchToIndex(hTmpCur: hDbiCur; IdxName: string); begin Check(DbiSwitchToIndex(hTmpCur, PChar(IdxName), nil, 0, True)); end; //************************************************************************************* Decode a TIME variable into hour, minutes, and seconds. This example decodes Hour, Minute, and Seconds fields from a the TIME value specified in the TimeT parameter and returns the time value as a string. This example uses the following input: TimeStr := fDbiTimeDecode(MyTime, MyHour, MyMin, MyMilSec); The function is: function fDbiTimeDecode(TimeT: Time; var iHour, iMin, iSec: Word): string; begin Check(DbiTimeDecode(TimeT, iHour, iMin, iSec)); iSec := iSec div 1000; SetLength(Result, 12); if (iHour < 12) then begin if (iHour = 0) then iHour := 12; Result := Format('%d:%d:%d AM', [iHour, iMin, iSec]); end else begin if (iHour > 12) then dec(iHour, 12); Result := Format('%d:%d:%d PM', [iHour, iMin, iSec]); end; SetLength(Result, StrLen(PChar(Result))); end;