C# 如何使用DatabaseExpert的SQL命令以编程方式更改报表中CrystalReports数据源的位置?

C# 如何使用DatabaseExpert的SQL命令以编程方式更改报表中CrystalReports数据源的位置?,c#,asp.net,sql-server,crystal-reports,C#,Asp.net,Sql Server,Crystal Reports,我正在使用VisualStudio中DatabaseExpert的SQL命令创建一个报告(CrystalReports11)。因此,负责从数据库中检索信息的查询位于rpt文件中 我想在我的web应用程序(C#)中以PDF文档的形式打开它。当我的应用程序的数据库连接字符串与报表设计器中使用的相同时,它就可以工作。如果在Web.config中配置了其他数据库,则会出现以下错误: [COMException (0x80004005): The system cannot find the file s

我正在使用VisualStudio中DatabaseExpert的SQL命令创建一个报告(CrystalReports11)。因此,负责从数据库中检索信息的查询位于rpt文件中

我想在我的web应用程序(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    
用于将报告作为PDF文档导出到HttpResponse的代码。通过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;
    }
}

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

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

但是,您可以使用以下方法将现有报表数据库指向新的报表数据库。


希望这会有所帮助。

在我的场景中,我有两个结构相同的数据库DB1和DB2。我使用指向DB1的SQL命令创建了报告。当我将web应用程序的配置更改为指向DB2时,会抛出错误。因此,我认为数据库、端口和SQL查询在报表中是硬编码的。如果是这样,您应该能够在运行时更改数据源。我自己也做过。只需再次检查您的编码,并查看下面的链接。