首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用数据库专家的SQL命令以编程方式更改报表中的Crystal Reports数据源位置?

如何使用数据库专家的SQL命令以编程方式更改报表中的Crystal Reports数据源位置?
EN

Stack Overflow用户
提问于 2017-04-03 19:32:32
回答 1查看 3.5K关注 0票数 0

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

我想打开它在我的网页应用程序(C#)作为一个PDF文档。当我的应用程序的数据库连接字符串与报表设计器中使用的字符串相同时,它可以工作。当在Web.config中配置了不同的数据库时,我会得到以下错误:

代码语言:javascript
复制
[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    

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

代码语言:javascript
复制
        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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-04 09:51:10

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

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

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

希望这能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43192844

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档