偶尔看源代码,发现TSQLCustomDataSet.RecordCount是用 select count(*) from xxx 查出来的.
为何要再查一下,虽然是单向的,抓来的数据放哪了?不能直接得到记录数吗?
顺便散分

解决方案 »

  1.   

    TSQLxxx是不是bde的那一套?
    这样做,是不是因为取记录是分批背景取的?所以无法根据已经取得的记录数立即得到recordcount
      

  2.   

    上源代码function GetRows(Query: string; Connection: TSQLConnection): Integer;
    var
      DS: TSQLDataSet;
    begin
      Result := -1;
      DS := TSQLDataSet.Create(nil);
      try
        DS.SQLConnection := Connection;
        DS.CommandText := Query;
        DS.Active := True;
        if not DS.EOF then
          Result := DS.Fields[0].AsInteger;
      finally
        DS.Free;
        if Result = -1 then
          DatabaseError(SNotSupported);
      end;
    end;function TCustomSQLDataSet.GetRecordCount: Integer;
    const
      SDistinct = ' distinct ';                 { do not localize }
      SSelectCount = 'select count(*) from ';   { do not localize }
    var
      TableName, Query: string;
      HoldPos: Integer;
    begin
      if FRecords <> -1 then
        Result := FRecords
      else
      begin
        CheckConnection(eConnect);
        if Self.CommandText = '' then
          DatabaseError(SNoSQLStatement);
        case CommandType of
          ctServerMethod,
          ctStoredProc:
            DatabaseError(SNotSupported);
          ctTable:
            begin
              with GetInternalConnection.FDBXConnection do
              begin
                //Query := 'select count(*) from ' + GetQuoteChar + FCommandText + GetQuoteChar;
                Query := 'select count(*) from ' + QuoteIdentifier(CommandText, false);
              end;
            end;
          ctQuery:
            begin
              TableName := GetTableNameFromSQLEx(FCommandText, GetIdOption(FSQLConnection));
              if (TableName = '') or (Params.Count > 0) then
                DatabaseError(SNotSupported);
              if Pos(SDistinct, LowerCase(FCommandText)) = 0 then
                Query := SSelectCount
              else
                DatabaseError(SNotSupported);
              HoldPos := Pos(SWhere, LowerCase(FCommandText));
              if HoldPos = 0 then
                Query := Query + GetQuoteChar + TableName + GetQuoteChar
              else begin
                Query := Query + GetQuoteChar + TableName + GetQuoteChar + copy(FCommandText, HoldPos, Length(FCommandText) - (HoldPos-1));
                HoldPos := Pos(sOrderBy, LowerCase(Query));
                if HoldPos > 0 then
                  Query := copy(Query, 1, HoldPos - 1);
              end;
            end;
        end;
        FRecords := GetRows(Query, FSQLConnection); //这行看上面
        Result := FRecords;  
      end;
    end;
      

  3.   

    晕,
    应该是这样
    TCustomSQLDataSet
    而不是
    TSQLCustomDataSet