簡體   English   中英

如何在Visual Studio 2013中查看Crystal報表?

[英]How to View Crystal report in Visual Studio 2013?

我正在ASP.Net與C#,並安裝CrystalReport 13適用於Visual Studio 2013年我寫代碼來填充一個Datasource ,並把它傳遞到我的`CrystalReport文件,但我的網站不顯示任何東西。 如何執行報告?

這是我的PageLoad:

SqlConnection cnn = new SqlConnection(connectionstring);
cnn.Open();
SqlCommand command = new SqlCommand();
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
command.Connection = cnn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "STP_T00050030";
command.Parameters.Add("@F_CNTRowID", SqlDbType.Int).Value = Convert.ToInt32("20");
command.Parameters.Add("@F_CNTid", SqlDbType.Int).Value = Convert.ToInt32("45");
adapter = new SqlDataAdapter(command);
adapter.Fill(ds);

ReportDocument crystalReport = new ReportDocument();
crystalReport.Load(Server.MapPath("~/Report1.rpt"));
crystalReport.SetDataSource(ds);
CrystalReportViewer1.ReportSource = crystalReport;
  1. 在您的解決方案中,右鍵單擊每個報告(.rpt文件)並轉到屬性-> Build Action = Content,復制到Output Directory = Copy Always

  2. 這是用於設置數據源的完整代碼塊。 請注意,還必須如何為每個子報表設置數據源。

     public static CrystalReportSource Crystal_SetDataSource(string reportName, object par1, object par2, object par3) { CrystalReportSource CrystalReportSource2 = new CrystalReportSource(); CrystalReportSource2.CacheDuration = 10; CrystalReportSource2.Report.FileName = @"~\\App_Pages\\Secure\\Reports\\" + reportName; CrystalDecisions.CrystalReports.Engine.Database crDatabase = CrystalReportSource2.ReportDocument.Database; ConnectionInfo crConnectionInfo = new ConnectionInfo(); crConnectionInfo.ServerName = server; crConnectionInfo.DatabaseName = db; crConnectionInfo.UserID = crystalUser; crConnectionInfo.Password = pwd; crConnectionInfo.IntegratedSecurity = false; // First we assign the connection to all tables in the main report TableLogOnInfo crTableLogOnInfo = new TableLogOnInfo(); foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crDatabase.Tables) { crTableLogOnInfo = crTable.LogOnInfo; crTableLogOnInfo.ConnectionInfo = crConnectionInfo; crTable.ApplyLogOnInfo(crTableLogOnInfo); } //SET DATASOURCE FOR EACH SUBREPORT IN REPORT foreach (CrystalDecisions.CrystalReports.Engine.Section section in CrystalReportSource2.ReportDocument.ReportDefinition.Sections) { // In each section we need to loop through all the reporting objects foreach (CrystalDecisions.CrystalReports.Engine.ReportObject reportObject in section.ReportObjects) { if (reportObject.Kind == ReportObjectKind.SubreportObject) { SubreportObject subReport = (SubreportObject)reportObject; ReportDocument subDocument = subReport.OpenSubreport(subReport.SubreportName); foreach (CrystalDecisions.CrystalReports.Engine.Table table in subDocument.Database.Tables) { // Cache the logon info block TableLogOnInfo logOnInfo = table.LogOnInfo; // Set the connection logOnInfo.ConnectionInfo = crConnectionInfo; // Apply the connection to the table! table.ApplyLogOnInfo(logOnInfo); } } } } if (CrystalReportSource2.ReportDocument.DataSourceConnections.Count > 0) CrystalReportSource2.ReportDocument.DataSourceConnections[0].SetConnection(server, db, crystalUser, pwd); CrystalReportSource2.ReportDocument.SetDatabaseLogon(crystalUser, pwd, server, db); if (par1 != null) CrystalReportSource2.ReportDocument.SetParameterValue(0, par1); if (par2 != null) CrystalReportSource2.ReportDocument.SetParameterValue(1, par2); if (par3 != null) CrystalReportSource2.ReportDocument.SetParameterValue(2, par3); return CrystalReportSource2; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM