delphi7找不到TBDEClientDataSet控件的解决方案

2016-02-19 16:22 33 1 收藏

有一种朋友不在生活里,却在生命力;有一种陪伴不在身边,却在心间。图老师即在大家的生活中又在身边。这么贴心的服务你感受到了吗?话不多说下面就和大家分享delphi7找不到TBDEClientDataSet控件的解决方案吧。

【 tulaoshi.com - 编程语言 】

 

  unit BDEClientDataSet;

  interface

  uses Windows, SysUtils, Variants, Classes, DB, DBCommon, Midas,
  SqlTimSt, DBClient, DBLocal, Provider, DBTables;

  
  type
  { TBDEQuery }

    TBDEQuery = class(TQuery)
    private
      FKeyFields: string;
    protected
      function PSGetDefaultOrder: TIndexDef; override;
    end;

  { TBDEClientDataSet }
    TBDEClientDataSet = class(TCustomCachedDataSet)
    private
      FCommandText: string;
      FCurrentCommand: string;
      FDataSet: TBDEQuery;
      FDatabase: TDataBase;
      FLocalParams: TParams;
      FStreamedActive: Boolean;
      procedure CheckMasterSourceActive(MasterSource: TDataSource);
      procedure SetDetailsActive(Value: Boolean);
      function GetConnection: TDataBase;
      function GetDataSet: TDataSet;
      function GetMasterSource: TDataSource;
      function GetMasterFields: string;
      procedure SetConnection(Value: TDataBase);
      procedure SetDataSource(Value: TDataSource);
      procedure SetLocalParams;
      procedure SetMasterFields(const Value: string);
      procedure SetParamsFromSQL(const Value: string);
      procedure SetSQL(const Value: string);
    protected
      function GetCommandText: String; override;
      procedure Loaded; override;
      procedure Notification(AComponent: TComponent; Operation: TOperation); override;
      procedure SetActive(Value: Boolean); override;
      procedure SetCommandText(Value: string); override;
    public
      constructor Create(AOwner: TComponent); override;
      destructor Destroy; override;
      procedure CloneCursor(Source: TCustomClientDataSet; Reset: Boolean;
        KeepSettings: Boolean = False); override;
      procedure GetFieldNames(List: TStrings); override;
      function GetQuoteChar: String;
      property DataSet: TDataSet read GetDataSet;
    published
      property Active;
      property CommandText: string read GetCommandText write SetCommandText;
      property DBConnection: TDataBase read GetConnection write SetConnection;
      property MasterFields read GetMasterFields write SetMasterFields;
      property MasterSource: TDataSource read GetMasterSource write SetDataSource;
    end;
   
  procedure Register;

  implementation

  uses BDEConst, MidConst;

  type

  { TBDECDSParams }

    TBDECDSParams = class(TParams)
    private
      FFieldName: TStrings;
    protected
      procedure ParseSelect(SQL: string);
    public
      constructor Create(Owner: TPersistent);
      Destructor Destroy; override;
    end;

  constructor TBDECDSParams.Create(Owner: TPersistent);
  begin
    inherited;
    FFieldName := TStringList.Create;
  end;

  destructor TBDECDSParams.Destroy;
  begin
    FreeAndNil(FFieldName);
    inherited;
  end;

  procedure TBDECDSParams.ParseSelect(SQL: string);
  const
    SSelect = 'select';
  var
    FWhereFound: Boolean;
    Start: PChar;
    FName, Value: string;
    SQLToken, CurSection, LastToken: TSQLToken;
    Params: Integer;
  begin
    if Pos(' ' + SSelect + ' ', LowerCase(string(PChar(SQL)+8))) 1 then Exit;  // can't parse sub queries
    Start := PChar(ParseSQL(PChar(SQL), True));
    CurSection := stUnknown;
    LastToken := stUnknown;
    FWhereFound := False;
    Params := 0;
    repeat
      repeat
        SQLToken := NextSQLToken(Start, FName, CurSection);
        if SQLToken in [stWhere] then
        begin
          FWhereFound := True;
          LastToken := stWhere;
        end else if SQLToken in [stTableName] then
        begin
          { Check for owner qualified table name }
          if Start^ = '.' then
            NextSQLToken(Start, FName, CurSection);
        end else
        if (SQLToken = stValue) and (LastToken = stWhere) then
          SQLToken := stFieldName;
        if SQLToken in SQLSections then CurSection := SQLToken;
      until SQLToken in [stFieldName, stEnd];
      if FWhereFound and (SQLToken in [stFieldName]) then
        repeat
          SQLToken := NextSQLToken(Start, Value, CurSection);
            if SQLToken in SQLSections then CurSection := SQLToken;
        until SQLToken in [stEnd,stValue,stIsNull,stIsNotNull,stFieldName];
      if Value='?' then
      begin
        FFieldName.Add(FName);
        Inc(Params);
      end;
    until (Params = Count) or (SQLToken in [stEnd]);
  end;

  { TBDEQuery }

    function TBDEQuery.PSGetDefaultOrder: TIndexDef;
    begin
      if FKeyFields = '' then
        Result := inherited PSGetDefaultOrder
      else
      begin  // detail table default order
        Result := TIndexDef.Create(nil);
        Result.Options := [ixUnique];      // keyfield is unique
        Result.Name := StringReplace(FKeyFields, ';', '_', [rfReplaceAll]);
        Result.Fields := FKeyFields;
      end;
    end;

  { TBDEClientDataSet }

  constructor TBDEClientDataSet.Create(AOwner: TComponent);
  begin
    inherited Create(AOwner);
    FDataSet := TBDEQuery.Create(nil);
    FDataSet.Name := Self.Name + 'DataSet1';
    Provider.DataSet := FDataSet;
    SqlDBType := typeBDE;
    FLocalParams := TParams.Create;
  end;

  destructor TBDEClientDataSet.Destroy;
  begin
    FreeAndNil(FLocalParams);
    FDataSet.Close;
    FreeAndNil(FDataSet);
    inherited Destroy;
  end;

  procedure TBDEClientDataSet.GetFieldNames(List: TStrings);
  var
    Opened: Boolean;
  begin
    Opened := (Active = False);
    try
      if Opened then
        Open;
      inherited GetFieldNames(List);
    finally
      if Opened then Close;
    end;
  end;

  function TBDEClientDataSet.GetCommandText: string;
  begin
    Result := FCommandText;
  end;

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  function TBDEClientDataSet.GetDataSet: TDataSet;
  begin
    Result := FDataSet as TDataSet;
  end;

  procedure TBDEClientDataSet.CheckMasterSourceActive(MasterSource: TDataSource);
  begin
    if Assigned(MasterSource) and Assigned(MasterSource.DataSet) then
      if not MasterSource.DataSet.Active then
        DatabaseError(SMasterNotOpen);
  end;

  procedure TBDEClientDataSet.SetParamsFromSQL(const Value: string);
  var
    DataSet: TQuery;
    TableName, TempQuery, Q: string;
    List: TBDECDSParams;
    I: Integer;
    Field: TField;
  begin
    TableName := GetTableNameFromSQL(Value);
    if TableName '' then
    begin
      TempQuery := Value;
      List := TBDECDSParams.Create(Self);
      try
        List.ParseSelect(TempQuery);
          List.AssignValues(Params);
        for I := 0 to List.Count - 1 do
          List[I].ParamType := ptInput;
        DataSet := TQuery.Create(nil);
        try
          DataSet.DatabaseName := FDataSet.DatabaseName;
          Q := GetQuoteChar;
          DataSet.SQL.Add('select * from ' + Q + TableName + Q + ' where 0 = 1'); { do not localize }
          try
            DataSet.Open;
            for I := 0 to List.Count - 1 do
            begin
              if List.FFieldName.Count I then
              begin
                try
                  Field := DataSet.FieldByName(List.FFieldName[I]);
                except
                  Field := nil;
                end;
              end else
                Field := nil;
              if Assigned(Field) then
              begin
                if Field.DataType ftString then
                  List[I].DataType := Field.DataType
                else if TStringField(Field).FixedChar then
                  List[I].DataType := ftFixedChar
                else
                  List[I].DataType := ftString;
              end;
            end;
          except
            // ignore all exceptions
          end;
        finally
          DataSet.Free;
        end;
      finally
        if List.Count 0 then
          Params.Assign(List);
        List.Free;
      end;
    end;
  end;

  procedure TBDEClientDataSet.SetSQL(const Value: string);
  begin
    if Assigned(Provider.DataSet) then
    begin
      TQuery(Provider.DataSet).SQL.Clear;
      if Value '' then
        TQuery(Provider.DataSet).SQL.Add(Value);
      inherited SetCommandText(Value);
    end else
      DataBaseError(SNoDataProvider);
  end;

   

  procedure TBDEClientDataSet.Loaded;
  begin
    inherited Loaded;
    if FStreamedActive then
    begin
      SetActive(True);
      FStreamedActive := False;
    end; 
  end;

  function TBDEClientDataSet.GetMasterFields: string;
  begin
    Result := inherited MasterFields;
  end;

  procedure TBDEClientDataSet.SetMasterFields(const Value: string);
  begin
    inherited MasterFields := Value;
    if Value '' then
      IndexFieldNames := Value;
    FDataSet.FKeyFields := '';
  end;

  procedure TBDEClientDataSet.SetCommandText(Value: String);
  begin
    inherited SetCommandText(Value);
    FCommandText := Value;
    if not (csLoading in ComponentState) then
    begin
      FDataSet.FKeyFields := '';
      IndexFieldNames := '';
      MasterFields := '';
      IndexName := '';
      IndexDefs.Clear;
      Params.Clear;
      if (csDesigning in ComponentState) and (Value '') then
        SetParamsFromSQL(Value);
    end;
  end;

  function TBDEClientDataSet.GetConnection: TDatabase;
  begin
    Result := FDataBase;
  end;

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  procedure TBDEClientDataSet.SetConnection(Value: TDataBase);
  begin
    if Value = FDatabase then exit;
    CheckInactive;
    if Assigned(Value) then
    begin
      if not (csLoading in ComponentState) and (Value.DatabaseName = '') then
        DatabaseError(SDatabaseNameMissing);
      FDataSet.DatabaseName := Value.DatabaseName;
    end else
      FDataSet.DataBaseName := '';
    FDataBase := Value;
  end;

  function TBDEClientDataSet.GetQuoteChar: String;
  begin
    Result := '';
    if Assigned(FDataSet) then
      Result := FDataSet.PSGetQuoteChar;
  end;

  procedure TBDEClientDataSet.CloneCursor(Source: TCustomClientDataSet; Reset: Boolean;
     KeepSettings: Boolean = False);
  begin
    if not (Source is TBDEClientDataSet) then
      DatabaseError(SInvalidClone);
    Provider.DataSet := TBDEClientDataSet(Source).Provider.DataSet;
    DBConnection := TBDEClientDataSet(Source).DBConnection;
    CommandText := TBDEClientDataSet(Source).CommandText;
    inherited CloneCursor(Source, Reset, KeepSettings);
  end;

  procedure TBDEClientDataSet.Notification(AComponent: TComponent; Operation: TOperation);
  begin
    inherited Notification(AComponent, Operation);
    if Operation = opRemove then
      if AComponent = FDatabase then
      begin
        FDataBase := nil;
        SetActive(False);
      end;
  end;

  procedure TBDEClientDataSet.SetLocalParams;

    procedure CreateParamsFromMasterFields(Create: Boolean);
    var
      I: Integer;
      List: TStrings;
    begin
      List := TStringList.Create;
      try
        if Create then
          FLocalParams.Clear;
        FDataSet.FKeyFields := MasterFields;
        List.CommaText := MasterFields;
        for I := 0 to List.Count -1 do
        begin
          if Create then
            FLocalParams.CreateParam( ftUnknown, MasterSource.DataSet.FieldByName(List[I]).FieldName,
                       ptInput);
          FLocalParams[I].AssignField(MasterSource.DataSet.FieldByName(List[I]));
        end;
      finally
        List.Free;
      end;
    end;

  begin
    if (MasterFields '') and Assigned(MasterSource) and Assigned(MasterSource.DataSet) then
    begin
      CreateParamsFromMasterFields(True);
      FCurrentCommand := AddParamSQLForDetail(FLocalParams, CommandText, True, GetQuoteChar);
    end;
  end;

  procedure TBDEClientDataSet.SetDataSource(Value: TDataSource);
  begin
    inherited MasterSource := Value;
    if Assigned(Value) then
    begin
      if PacketRecords = -1 then PacketRecords := 0;
    end else
    begin
      if PacketRecords = 0 then PacketRecords := -1;
    end;
  end;

  function TBDEClientDataSet.GetMasterSource: TDataSource;
  begin
    Result := inherited MasterSource;
  end;

  procedure TBDEClientDataSet.SetDetailsActive(Value: Boolean);
  var
    DetailList: TList;
    I: Integer;
  begin
    DetailList := TList.Create;
    try
      GetDetailDataSets(DetailList);
      for I := 0 to DetailList.Count -1 do
      if TDataSet(DetailList[I]) is TBDEClientDataSet then
        TBDEClientDataSet(TDataSet(DetailList[I])).Active := Value;
    finally
      DetailList.Free;
    end;
  end;

  procedure TBDEClientDataSet.SetActive(Value: Boolean);
  begin
    if Value then
    begin
      if csLoading in ComponentState then
      begin
        FStreamedActive := True;
        exit;
      end;
      if MasterFields '' then
      begin
        if not (csLoading in ComponentState) then
          CheckMasterSourceActive(MasterSource);
        SetLocalParams;
        SetSQL(FCurrentCommand);
        Params := FLocalParams;
        FetchParams;
      end else
      begin
        SetSQL(FCommandText);
        if Params.Count 0 then
        begin
          FDataSet.Params := Params;
          FetchParams;
        end;
      end;
    end;
    if Value and (FDataSet.ObjectView ObjectView) then
      FDataSet.ObjectView := ObjectView;
    inherited SetActive(Value);
    SetDetailsActive(Value);
  end;

  procedure Register;
  begin
    RegisterComponents('BDE', [TBDEClientDataSet]);
  end;

  end.
  
  //以上经DBLocalB.pas改装而成,可存为任意文件名,当然扩展名是PAS
  //然后安装此控件即可

来源:http://www.tulaoshi.com/n/20160219/1611860.html

延伸阅读
标签: Delphi
界面设计 首先,添加NMftp1、Edit、opendialog1、meno1、label等控件,按下图放置。 控件代码 1.“连接”代码: procedure TForm1.Button2Click(Sender: TObject); begin nmftp1.Host :=edit3.Text ; nmftp1.UserID :=edit1.text; nmftp1.Password :=edit2.text; nmftp1.Connect ; end; 2.“上传”代码: proc...
win7找不到运行框的解决方法   一、Win7运行在哪里 寻找命令入口Tulaoshi.com--找到windows7运行命令入口:所有程序--附件--命令提示符。(此功能与运行的功能基本相同)其实命令提示符就是运行。 二、Win7开始菜单找不到运行 1、右键鼠标点开始按钮,选择属性,按自定义按钮; 2、在运行命令前的选择框内...
标签: Web开发
一、使用iframe,通过document.write产生历史 代码如下: !DOCTYPE html html head meta charset="utf-8"/ title0/title /head body input type="button" value="加1" onclick="add()" / div id="info" style="border:red 1px solid;width:200px;padding:10px;margin:5px;"0/div /body /html script src="history-0.1.js"/script ...
随着中小企业在我国的蓬勃发展,越来越多的网络服务器会逐步进入千千万万的中小企业,与此同时,很多大的企业、机构随着业务的扩充分支机构也不断增加,这些用户需要购置网络服务器来满足业务发展的需要。但是这些用户受企业状况、资金预算的限制往往无法购置价格昂贵的基于SCSI接口的网络服务器,他们通常会选择性能较差、无冗余支持的ID...
计算系统与信息网络不停顿的运行与连接即高可用性已成为各行业特别是要求实时行业业务运行的基本要求。 惠普凭借丰富的经验创制的HP NetServer为您提供当今市场上最完备的高可用性系列产品和最优质的服务,使您的关键业务应用程序能连续可靠地高效运转。惠普还与业界领先的软硬件供应商联袂,为优化您的业务环境提供最...

经验教程

660

收藏

8
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部