频道分类

Delphi通过ADOQuery控件实现Sqlserver数据库多结果集的数据打印

作者:admin 来源: 日期:2020/2/8 13:59:34 人气: 标签:

 
//引用单元:
TADOQuery----------ADODB
TppDBPipeline-----------ppDBPipe
TDataSource-----------DB
Tppreport-----------ppReport
TppDesigner-----------ppEndUsr
//打印调用
procedure TfrmMain.N1Click(Sender: TObject);
begin
    strsql := ' select top 1 * from ZY_BRXXK '
            + ' select top 1 * from YY_YBFLK '
            + ' select top 1 * from ZY_BRSYK '
            + ' select top 1 * from ZY_BRXXK '
            + ' select top 1 * from ZY_BRXXK ';
    dm.print(strsql,'zyb_brxxcx_temple.rtm');
end;
//打印函数声明
//TADOQuery多结果集打印
procedure TDM.Print(asql, strMb: string);
    procedure QueryToPrint(aAdoQuqery :TADOQuery;aTemplateName :string);
    var
        aAdoDataSet :TADODataSet;
        i,j :Integer;
        appDBpipeLine :TppDBPipeline;
        aDataSource :TDataSource;
        ppDBPipleline1 :TppDBPipeline;
       // raCodeModule1:TraCodeModule;
        isMasterPipeline :Boolean;
        ppreportTmp: Tppreport; //报表控件
        ppDesignerTmp: TppDesigner;
    begin
        ppreportTmp := Tppreport.Create(Self);
        ppDesignerTmp:= TppDesigner.Create(Self);
        i :=0;
        isMasterPipeline :=True;
        if  Assigned(ppDBPipleline1) then
        begin
             FreeAndNil(ppDBPipleline1);
        end;
        //raCodeModule1:=TraCodeModule.Create(nil);
        ppDBPipleline1 :=TppDBPipeline.Create(self);
        ppDBPipleline1.Name :='BDEPipeline1';   //为了和外挂报表中一致,因此此处Name属性不可修改
        aAdoDataSet :=TADODataSet.Create(self);
        aAdoDataSet.Recordset :=aAdoQuqery.Recordset;
        
        while aAdoDataSet.Recordset<>nil do
        begin
            if isMasterPipeline then
            begin
                aDataSource :=TDataSource.Create(self);
                ppDBPipleline1.DataSource :=aDataSource;
                ppDBPipleline1.DataSource.DataSet :=aAdoDataSet;
            end
            else
            begin
                aDataSource :=TDataSource.Create(self);
                appDBpipeLine :=TppDBPipeline.Create(self);
                appDBpipeLine.DataSource :=aDataSource;
                appDBpipeLine.DataSource.DataSet :=aAdoDataSet;
            end;
 
            aAdoDataSet :=TADODataSet.Create(self);
            aAdoDataSet.Recordset :=aAdoQuqery.NextRecordset(j);
            if not isMasterPipeline then
            begin
                i :=i+1;
                appDBpipeLine.Name :='p_ppbdepipeline'+Inttostr(i);  //为了和外挂报表中一致,因此此处Name属性不可修改
            end;
 
            isMasterPipeline :=False;
        end;
        ppreportTmp.DataPipeline :=ppDBPipleline1;
        ppreportTmp.Template.FileName :='c:\'+aTemplateName;//模板路径
        if not FileExists('c:\'+aTemplateName) then
        begin
            ppDesignerTmp.Report := ppreportTmp;
            ppDesignerTmp.ShowModal;
        end
        else
        begin
            ppreportTmp.Template.LoadFromFile;
 
            if true then//预览
            begin
                ppDesignerTmp.Report := ppreportTmp;
                ppDesignerTmp.ShowModal;
            end
            else
            begin
                ppreportTmp.Print;
            end;
        end;
        ppDBPipleline1.Free;
        ppDBPipleline1 :=nil;
        //raCodeModule1.Free;
        //raCodeModule1:=nil;
 
        for i:=self.ComponentCount -1 downto 0 do
        begin
            if (Pos('p_ppbdepipeline',self.Components[i].Name)>0) and (Self.Components[i] is TppDBPipeline) then
            begin
                appDBpipeLine :=TppDBPipeline(self.Components[i]);
                appDBpipeLine.DataSource.DataSet.Free;
                appDBpipeLine.DataSource.DataSet :=nil;
                appDBpipeLine.DataSource.Free;
                appDBpipeLine.DataSource :=nil;
                appDBpipeLine.Free;
                appDBpipeLine :=nil;
            end;
        end;
        ppreportTmp.Free;
        ppDesignerTmp.Free;
 
    end;
var
    PrintTempleate :string;
    strsql:string;
    qryTemp: TADOQuery;
    username, password, drivename,servername, databasename: string;
begin
    qryTemp:=TADOQuery.Create(application);
    qryTemp.Active:=False;
    //ztapp.ztserver.getserverinfo('dsquery', servername, username, databasename, password, drivename);
    //多个打印结果时用 ADO 控件 qryTemp(赋值mssql的数据库连接)
    qryTemp.ConnectionString :='Provider=SQLOLEDB.1;Password='+password
                +';Persist Security Info=True;User ID='+username+';Initial Catalog='+databasename+';'
                +'Data Source='+servername;
    strsql:= asql;
    PrintTempleate :=strMb;
//    if not FileExists(ztapp.ReportTemplatePath+strMb) then
//    begin
//        ShowMessage('文件:'+ztapp.ReportTemplatePath+strMb+'不存在');
//        Exit;
//    end;
 
    qryTemp.Active:=False;
    qryTemp.Close;
    qryTemp.SQL.Clear;
    strsql:= strsql +' set QUOTED_IDENTIFIER OFF select 'XXXX' as [当前操作员],"'
    +FormatDateTime('yyyy-mm-ddhh:mm:ss',Now)+'YYYY' as [当前操作员科室] ';
    qryTemp.SQL.Add(strsql);
 
    try
        qryTemp.Open;
    except
        showmessage('执行SQL'+strsql+'异常!');
        Exit;
    end;
    if qryTemp.Fields[0].AsString ='F' then
    begin
        showmessage(qryTemp.Fields[1].AsString);
        Exit;
    end;
 
    QueryToPrint(qryTemp,PrintTempleate);
    qryTemp.Free;
end;

来源:https://blog.csdn.net/weixin_41660162/article/details/79828210