2017-04-03 5 views
0

Visual StudioのデータベースエキスパートのSQLコマンドを使用してレポート(Crystal Reports 11)を作成しています。したがって、データベースから情報を取得するクエリは、rptファイル内にあります。データベースエキスパートのSQLコマンドを使用して、レポートでCrystal Reportsのデータソースの場所をプログラムで変更する方法はありますか。

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; 
    } 
} 

答えて

0

Crystalレポートを作成して実行すると、SQLクエリはレポート内でハードコードされます。データベースメニューの "Show SQL Query"でチェックすることができます。

その後、サーバー名はC#コードでのみ変更できます。あなたのデータベース名&テーブルは同じでなければなりません、そうでないとエラーが発生します。したがって、異なるデータベースを使用している場合は、2つ以上のレポートを作成することをお勧めします。

ただし、以下の方法を使用して既存のレポートデータベースを新しいものにすることができます。 Point Crystal Reports at a new database

希望すると、これが役立ちます。

+0

私のシナリオでは、同じ構造のDB1とDB2を持つ2つのデータベースがあります。 DB1を指すSQLコマンドでレポートを作成しました。 DB2を指すようにWebアプリケーションの構成を変更すると、エラーがスローされます。だから、私は、データベース、ポートとSQLのクエリは、レポート内でハードコードされていることをお勧めします。 –

+0

その場合、実行時にデータソースを変更できるはずです。私はそれを自分でやった。コーディングをもう一度チェックし、下のリンクも参照してください。 https://www.codeproject.com/Articles/270977/Change-the-data-source-of-a-Crystal-Report-at-Run – samithagun

関連する問題