Einzelnen Beitrag anzeigen

Schwarzesshaaf666

Registriert seit: 14. Apr 2006
Ort: Herford
13 Beiträge
 
#17

AW: ADO Queries per Thread aktualisieren

  Alt Gestern, 08:55
Nein, bewusst war mir das nicht. Hab nach dem genannten Code gesucht und ihn hier gefunden. Hab ihn noch etwas angepasst, da ich die Threads irgendwie in OnTerminate identifizieren muss.

Delphi-Quellcode:
type
  TFieldInfoRecord = Record
    DataType: TFieldType;
    Name: String;
    Size: Integer;
    Value: Variant;
    Attributes: TParameterAttributes;
  End;

  TDBThread = class(TThread)
    constructor Create(Const AConnectionString, ASql: string; FDArray: TFieldInfoArray; ARightsPk: Integer);
  private
    FConnectionString, FSql: string;
    FRecordSet: _RecordSet;
    FRightsPk: Integer;
    FFDArray: TFieldInfoArray;
  protected
    procedure Execute; override;
  public
    property RecordSet: _RecordSet read FRecordSet;
    property RightsPk: Integer read FRightsPk write FRightsPk;
  end;

implementation

constructor TDBThread.Create(Const AConnectionString, ASql: string; FDArray: TFieldInfoArray; ARightsPk: Integer);
var
  i: Integer;
begin
  inherited Create(False);

  FConnectionString := AConnectionString;
  FSql := ASql;
  SetLength(FFDArray, Length(FDArray));
  for i := 0 to High(FDArray) do
  begin
    FFDArray[i].DataType := FDArray[i].DataType;
    FFDArray[i].Size := FDArray[i].Size;
    FFDArray[i].Name := FDArray[i].Name;
    FFDArray[i].Value := FDArray[i].Value;
    FFDArray[i].Attributes := FDArray[i].Attributes;
  end;
  FRightsPk := ARightsPk;
end;

procedure TDBThread.Execute;
var
  i: Integer;
begin
  inherited;

  CoInitialize(nil);
  try
    With TADODataSet.Create(nil) do
      try
        try
          CommandTimeOut := 600;
          ConnectionString := FConnectionString;
          Commandtext := FSql;
          Parameters.ParseSQL(FSql, True); // extract parameters
          for i := Low(FFDArray) to High(FFDArray) do // set parametervalues
          begin
            Parameters.ParamByName(FFDArray[i].Name).DataType := FFDArray[i].DataType;
            Parameters.ParamByName(FFDArray[i].Name).Size := FFDArray[i].Size;
            Parameters.ParamByName(FFDArray[i].Name).Value := FFDArray[i].Value;
            Parameters.ParamByName(FFDArray[i].Name).Attributes := FFDArray[i].Attributes;
          end;
          Open;
          FRecordSet := RecordSet; // keep recordset
        except
          on Ex: Exception do
            MessageDlg('Error in Thread: ' + Ex.Message + sLineBreak + 'SQL: ' + FSql, mtError, [mbOK], 0);
        end;
      finally
        Free;
      end;
  finally
    CoUninitialize;
  end;
end;
_________________
FREI STATT BAYERN
  Mit Zitat antworten Zitat