2011-02-25 67 views
0

水晶報表(.rpt)文件可以通過標準報表創建嚮導創建,此時用戶有兩個選項。可以添加命令或從數據庫中選擇表格。 您也可以從visual studio 2010創建水晶報表文件(.rpt) - >添加新的水晶報表 - >水晶報表。 現在我正在更改運行時的rpt文件的數據庫名稱帶命令的水晶報表

CrystalReportViewer crystalreportviewer = new CrystalReportViewer(); 
ReportDocument rd = new ReportDocument(); 
rd.Load(filepath);//filepath for rpt file c://command.rpt 
crystalreportviewer.ReportSource = rd; // set the source as reportdocument 

foreach (CrystalDecisions.CrystalReports.Engine.Table tb in rd.Database.Tables) 
     { 
      TableLogOnInfo tbloginfo = new TableLogOnInfo(); 
      CrystalDecisions.Shared.ConnectionInfo ci = new ConnectionInfo(); 
      ci.DatabaseName = databasename; ***// Changing at run time.*** User can change databasename 
      ci.ServerName = "Myservername"; 

      ci.UserID = "userid"; 
      ci.Password = "userpassword"; 
      tbloginfo.ConnectionInfo = ci; 
      tb.ApplyLogOnInfo(tbloginfo); 
      try 
      { 

       tb.Location = ci.DatabaseName + ".dbo." + tb.Location.Substring(tb.Location.LastIndexOf(".") + 1); 
      } 
      catch 
      { 

      } 
     } 

當rpt文件使用命令時,代碼不起作用。在調試時,rd(reportdocument).DataBase.Tables [0] .Name =「Command」 如果rpt文件已經手動選擇了表 rd(reportdocument).DataBase.Tables [0] .Name =「EmployeeTable」表)

代碼在找到命令時不會更改表的connectionInfo。

請問我是否讓我自己清楚。

謝謝, Ñ頭像

回答

0

夫婦錯誤。 我正在使用CrystalDecisions.Windows.Forms.CrystalReportViewer現在我正在使用SAPBusinessObjects.WPF.Viewer.CrystalReportsViewer。

然後我說

crystalreportviewer.ViewerCore.RefreshReport(); 

問題解決了。 謝謝, N