水晶报表_避免多次查询数据库_提高速度

最近现场反映做的水晶报表打印时特别慢,调查后发现确实如此,

原来在web客户端打印时,点击“打印”按钮弹出打印机选择画面以及实际打印时,都需要重新绑定水晶报表(一般在绑定的方法中要操作数据库),

造成速度很慢。

 

对策:控制只操作一次数据库,将结果数据保存在session中。

注意print_Init会多次执行。

 

    Private Sub print_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        '显示水晶报表()    '翻页到第二页后不正对应
        'Me.ShowReport()  
        If Not IsPostBack Then
            Session("dtparms") = Nothing
        Else
            Me.ShowReport()
        End If

    End Sub

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Session(ConstantInfo.SESSION_USER_INFO_CONST) Is Nothing Then
                Me.Page.ClientScript.RegisterStartupScript(Me.GetType, "msg", "<script language='javascript'>parent.location.href='../login.aspx';</script>")
                Exit Sub
            End If

            Dim strKey = Request.Item("Key").ToString().Trim
            Select Case Request.Item("printName").ToString()
                Case "XXX.rpt"
                    Session("dtparms") = 取数据库函数(strKey)
                Case ""
            End Select

            Me.ShowReport(True)
        End If
    End Sub

    '显示水晶报表
    Public Sub ShowReport(Optional ByVal bolFirstLoadFlag As Boolean = False)
        Dim fileName As String
        Dim strKey As String
        Dim ds As New DataSet
        Dim dtparms As New DataTable
        fileName = Request.Item("printName").ToString()
        strKey = Request.Item("Key").ToString().Trim

        Select Case fileName
            Case "XXX.rpt"
                strTitle = "标题。。。。"

                If Session("dtparms") Is Nothing Then                   

                    dtparms = 取数据库函数(strKey) 

                    Session("dtparms") = dtparms

                Else
                    dtparms = CType(Session("dtparms"), DataTable)
                End If

                If dtparms Is Nothing OrElse dtparms.Rows.Count < 1 OrElse dtData Is Nothing Then
                    Exit Sub
                End If
                '设置水晶报表页面
                Me.CrystalReportSource1.ReportDocument.Load(Server.MapPath("/Aspx/XX/" + fileName))
                '设置水晶报表数据源
                Me.CrystalReportSource1.ReportDocument.SetDataSource(dtData)
                '设置水晶报表参数
                Me.CrystalReportSource1.ReportDocument.SetParameterValue("A", dtparms.Rows(0).Item("A").trim)
                ....
            Case ""
        End Select

        '绑定
        Me.CrystalReportSource1.DataBind()
        Me.CrystalReportViewer1.DataBind()

        '样式设定
        Me.CrystalReportViewer1.Visible = True
        '初次载入时,保存pdf
        If bolFirstLoadFlag Then
            SavePdf("要保存的PDF文件名.pdf")
        End If
        Me.CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX
        CrystalReportViewer1.BestFitPage = True
        CrystalReportViewer1.HasCrystalLogo = False
        CrystalReportViewer1.HasSearchButton = False
        'CrystalReportViewer1.HasExportButton = False
        CrystalReportViewer1.HasToggleGroupTreeButton = False
        CrystalReportViewer1.HasViewList = False
        CrystalReportViewer1.ToolbarStyle.Width = New Unit("950px")

        CrystalReportViewer1.ShowFirstPage()
    End Sub

    '保存PDF文档
    Private Sub SavePdf(ByVal strPdfFileName As String)
        Dim rDoc As New CrystalDecisions.CrystalReports.Engine.ReportDocument

        Try
            rDoc = Me.CrystalReportSource1.ReportDocument
            rDoc.ExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat
            rDoc.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
            Dim diskOpts As DiskFileDestinationOptions = New DiskFileDestinationOptions()
            diskOpts.DiskFileName = WebConfigurationManager.AppSettings("PdfFileFold").ToString + strPdfFileName
            rDoc.ExportOptions.DestinationOptions = diskOpts
            rDoc.Export()
        Catch ex As Exception

        End Try
    End Sub

    Private Function 取数据库函数(ByVal strOrderNo As String) As DataTable
        。。。
        Return table
    End Function

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值