2017-10-10 31 views
0

Iam使用c#構建windows應用程序並在其中包含水晶報表。無法在不同的計算機上安裝帶有水晶報表的C#windows應用程序

我的問題是,我使用的代碼,加載crystalreport.rpt文件從它的位置在開發應用程序的計算機中。

準確地在這行代碼中的問題。

  cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); 

現在每當我試圖運行在不同的計算機應用程序無法找到.rpt文件和我的報表不工作。

這裏是完整的代碼。

using System; 
using System.Windows.Forms; 
using CrystalDecisions.CrystalReports.Engine; 
using CrystalDecisions.Shared; 

namespace WindowsApplication1 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     ReportDocument cryRpt = new ReportDocument(); 
     TableLogOnInfos crtableLogoninfos = new TableLogOnInfos(); 
     TableLogOnInfo crtableLogoninfo = new TableLogOnInfo(); 
     ConnectionInfo crConnectionInfo = new ConnectionInfo(); 
     Tables CrTables ; 

     cryRpt.Load("PUT CRYSTAL REPORT PATH HERE\CrystalReport1.rpt"); 

     crConnectionInfo.ServerName = "YOUR SERVER NAME"; 
     crConnectionInfo.DatabaseName = "YOUR DATABASE NAME"; 
     crConnectionInfo.UserID = "YOUR DATABASE USERNAME"; 
     crConnectionInfo.Password = "YOUR DATABASE PASSWORD"; 

     CrTables = cryRpt.Database.Tables ; 
     foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in 
CrTables) 
     { 
      crtableLogoninfo = CrTable.LogOnInfo; 
      crtableLogoninfo.ConnectionInfo = crConnectionInfo; 
      CrTable.ApplyLogOnInfo(crtableLogoninfo); 
     } 

     crystalReportViewer1.ReportSource = cryRpt; 
     crystalReportViewer1.Refresh(); 
    } 
} 
} 

如何使我的報告在任何計算機上都能正常工作?

回答

0

你可以在app.config文件作爲鍵添加路徑和從您的代碼調用它

OR

System.Reflection.Assembly.GetExecutingAssembly() 

OR

System.IO.Path.GetFullPath 
相關問題