如何使用动态连接水晶报表

时间:2019-06-23 18:32:26

标签: c# wpf crystal-reports report crystal-reports-2008

我在我的应用程序中使用动态连接

当我在应用程序中使用本地数据库时,我的报告没有任何问题

但是当我收到报告时我使用另一个数据库(不本地)时,我的应用程序崩溃了

我使用c#和wpf

try
{
    ReportDocument rd = new ReportDocument();
    string path = System.AppDomain.CurrentDomain.BaseDirectory + "report\\" + this.report_name;
    rd.Load(path);
    rd.RecordSelectionFormula = this.GetFormula;

    switch (report_name)
    {
        case "full.rpt":
            {
                break;
            }
    }
}
catch
{
    MessageBox.Show("error");
}

我该怎么办?

1 个答案:

答案 0 :(得分:0)

 try
            {
                ReportDocument rd = new ReportDocument();
                //////////////////////
                TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
                TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
                ConnectionInfo crConnectionInfo = new ConnectionInfo();
                Tables CrTables;
                rd.Load(System.AppDomain.CurrentDomain.BaseDirectory + "report\\" + this.report_name);


                RegistryKey ConnectionKey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\forosh");
                string  server = (string)ConnectionKey.GetValue("ServerName");
                crConnectionInfo.ServerName = server;

                RegistryKey ConnectionKey2 = Registry.CurrentUser.CreateSubKey("SOFTWARE\\forosh");
                string db = (string)ConnectionKey2.GetValue("DbName");
                crConnectionInfo.DatabaseName = db;
                crConnectionInfo.IntegratedSecurity = true;

                CrTables = rd.Database.Tables;
                foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
                {
                    crtableLogoninfo = CrTable.LogOnInfo;
                    crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                    CrTable.ApplyLogOnInfo(crtableLogoninfo);
                }

                crv.ViewerCore.ReportSource = rd;
                crv.ViewerCore.RefreshReport();
                //////////////////////////////

                string path = System.AppDomain.CurrentDomain.BaseDirectory + "report\\" + this.report_name;
                rd.Load(path);
                rd.RecordSelectionFormula = this.GetFormula;

                switch (report_name)
                {

                    case "full.rpt":
                        {

                            break;
                        }
                    case "formul.rpt":
                        {

                            break;
                        }

                }


                crv.ViewerCore.ReportSource = rd;

            }
            catch {

            }
            }