如何在报表运行时建立连接

问题描述 投票:0回答:3

我想在 DevExpress 中发送报告连接,并发送对此报告的查询,我使用以下代码调用报告:

private void Form14_Load(object sender, EventArgs e)
    {
        try {
                            
            XtraReport report           = XtraReport.FromFile(@"C:\\a\\Report2.repx", true);
            ReportPrintTool printTool   = new ReportPrintTool(report);
            printTool.ShowPreviewDialog();

        }
        catch (Exception ex) {

            MessageBox.Show(ex.ToString());

        }
    }
c# devexpress
3个回答
0
投票

一般情况下,您的报表通过 SqlDataSource 组件连接到数据。因此,我建议您在 SqlDataSource 实例级别更新连接。为此,只需从 XtraReport 类获取现有数据源即可:

var sqlDataSource = xtraReport.DataSource as SqlDataSource;  
sqlDataSource.ConnectionParameters = ...; 

请参阅指定数据连接文章了解如何更改数据源的连接参数。


0
投票

您可以创建自己的报告实现,在其中可以通过检查报告和子报告中使用的连接字符串来更新连接字符串。

在下面的示例中,使用类型化数据集来创建报告,然后您可以在初始化时更新表适配器的连接字符串。

参考下面的实现: // 基础报告

    public partial class BaseReport: DevExpress.XtraReports.UI.XtraReport, IDisposable
    {

        public List<SQLiteConnection> GetConnectionList
        {
            get { return connectionList; }
        }

        public List<SimergyBaseReport> GetSubReportList
        {
            get { return subReportList; }

        }
        protected List<SQLiteConnection> connectionList = new List<SQLiteConnection>();
         protected List<SimergyBaseReport> subReportList = new List<SimergyBaseReport>();
         
        public void UpdateConnectionString(String connectionString)
        {
#if !RPT_DESIGN
            connectionString = GlobalUtilities.GetOutputConnectionString(connectionString);
            foreach (SQLiteConnection conn in connectionList)
            {
                conn.Close();
                conn.ConnectionString = connectionString;
                reportDataSet.Clear();
            }
            

            foreach (BaseReport subReport in comparisonSubReportList)
            {
                if (subReport != null)
                    subReport.UpdateConnectionString(connectionString);
            }            
#endif
        }
    }

//报告

public partial class UsageSummary : Simergy.Reporting.Reports.BaseReport
    {
        public ClimaticSummary()
        {
            InitializeComponent();
        }

        public ClimaticSummary(String connectionString, Model model)
            : base(model)
        {
            InitializeComponent();

            connectionList.Add(applicationUsageTableAdapter.Connection);
            connectionList.Add(dailyUsage_SUMMARYTableAdapter.Connection);
            subReportList.Add(xrSubreport1.ReportSource as BaseReport);
            subReportList.Add(xrSubreport2.ReportSource as BaseReport);
            subReportList.Add(footerSubReport.ReportSource as BaseReport);   
         
            reportDataSet = report_DataSet1;

            UpdateConnectionString(connectionString);            
        }
    }

// 自定义打印控件来呈现报告

public partial class ReportViewControl : PrintControl
{
    private SimergyBaseReport CurrentReport = null;
        public ReportViewControl()
        {
            InitializeComponent();
        }
        
        public void SetReport(SimergyBaseReport report)
        {
            CurrentReport = report;
            if (CurrentReport != null)
            {
                this.PrintingSystem = report.PrintingSystem;
                SetupButtonVisability();
                report.CreateDocument();
                report.RecreateDocumentMap();                
                this.PrintingSystem = report.PrintingSystem;               
            }
        }

        public void RefreshReport()
        {            
            this.PrintingSystem = null;
            this.PrintingSystem = CurrentReport.PrintingSystem;
            CurrentReport.CreateDocument(true);            
        }
    }

///用法

BaseReport report = new UsageSummary(someConnectionString);
ReportViewControl viewer = new ReportViewControl();
viewer.SetReport(report);
form1.Controls.Add(viewer);

0
投票

XtraReport1 报告 = new XtraReport1(); string sql =“从客户中选择*”;

© www.soinside.com 2019 - 2024. All rights reserved.