如何使用Database Expert中的SQL命令以编程方式在报表中更改Crystal Reports数据源位置?

时间:2017-04-03 19:32:33

标签: c# asp.net sql-server crystal-reports

我在Visual Studio中使用Database Expert中的SQL命令创建报表(Crystal Reports 11)。因此,负责从数据库检索信息的查询位于rpt文件中。

我想在我的网络应用程序(C#)中将其作为PDF文档打开。当我的应用程序的数据库连接字符串与报表设计器中使用的相同时,它可以工作。在Web.config中配置其他数据库时,出现以下错误:

[COMException (0x80004005): The system cannot find the file specified.]

CrystalDecisions.ReportAppServer.Controllers.ReportSourceClass.Export(ExportOptions pExportOptions, RequestContext pRequestContext) +0
CrystalDecisions.ReportSource.EromReportSourceBase.ExportToStream(ExportRequestContext reqContext) +1994
CrystalDecisions.CrystalReports.Engine.FormatEngine.ExportToStream(ExportRequestContext reqContext) +802
CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToStream(ExportOptions options) +231
CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToHttpResponse(ExportOptions options, HttpResponse response, Boolean asAttachment, String attachmentName) +403
CrystalDecisions.CrystalReports.Engine.ReportDocument.ExportToHttpResponse(ExportFormatType formatType, HttpResponse response, Boolean asAttachment, String attachmentName) +307
RelProducaoCDS.PreencherDados(Int64 codUsuario, Int64 codUnidade, DateTime datInicio, DateTime datFim) in d:\Projetos-NET\Projetos Net\Governa.Saude.AtencaoBasica\Governa.Saude.AtencaoBasica.Web\Relatorios\Producao\RelProducaoCDS.aspx.cs:110
RelProducaoCDS.Page_Load(Object sender, EventArgs e) in d:\Projetos-NET\Projetos Net\Governa.Saude.AtencaoBasica\Governa.Saude.AtencaoBasica.Web\Relatorios\Producao\RelProducaoCDS.aspx.cs:59
System.Web.UI.Control.LoadRecursive() +71
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178    

用于将报告导出为HttpResponse的代码作为PDF文档。通过ConfigurationManager类从Web.config检索连接字符串。

        Hashtable parameters = new Hashtable();
        parameters.Add("COD_USER", codUsuario);
        parameters.Add("DAT_START", datInicio);
        parameters.Add("DAT_END", datFim);

        ExportPDFToHttpResponse("myDatabaseConnection", "~\\Reports\\Production\\RelProductionCDS.rpt", parameters, new Hashtable());
    }

     public void ExportPDFToHttpResponse(string connectionName, string rptPath, Hashtable parameters, Hashtable parametersFormula)
    {
        ReportDocument rpt = CreateReportDocument(connectionName, rptPath);
        foreach (string key in parameters.Keys)
        {
            rpt.SetParameterValue(key, parameters[key]);
        }
        foreach (string key in parametersFormula.Keys)
        {
            rpt.DataDefinition.FormulaFields[key].Text = string.Concat("\"", parametersFormula[key] ,"\"");
        }
        string reportName = rptPath.Substring(0, rptPath.LastIndexOf('.'));
        reportName = reportName.Substring(reportName.LastIndexOf('\\'));
        rpt.ExportToHttpResponse(ExportFormatType.PortableDocFormat, Response, false, reportName);
        rpt.Close();
        rpt.Dispose();
    }

    private ReportDocument CreateReportDocument(string connectionString, string rptPath)
    {
        ReportDocument rpt = new ReportDocument();
        rpt.Load(Server.MapPath(rptPath));
        ConnectionInfo connInfo = new ConnectionInfo();

        string connString = ConfigurationManager.ConnectionStrings[connectionString].ConnectionString;
        SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connString);
        connInfo.ServerName = builder.DataSource;
        connInfo.DatabaseName = builder.InitialCatalog;
        connInfo.IntegratedSecurity = builder.IntegratedSecurity;
        if (!builder.IntegratedSecurity)
        {
            connInfo.Password = builder.Password;
            connInfo.UserID = builder.UserID;
        }

        Tables tables = rpt.Database.Tables;
        foreach (CrystalDecisions.CrystalReports.Engine.Table table in tables)
        {
            TableLogOnInfo tableLogOnInfo = table.LogOnInfo;
            tableLogOnInfo.ConnectionInfo = connInfo;
            table.ApplyLogOnInfo(tableLogOnInfo);
        }
        return rpt;
    }
}

1 个答案:

答案 0 :(得分:0)

创建水晶报告并运行后,SQL查询将在报告中进行硬编码。您可以通过“数据库”菜单中的“显示SQL查询”进行检查。

之后,您只能通过C#代码更改服务器名称。您的数据库名称&表必须相同,否则你会得到错误。因此,如果您有不同的数据库,我建议您创建2个或更多报告。

但是,您可以使用以下方法将现有报告数据库指向新数据库。 Point Crystal Reports at a new database

希望这会有所帮助。