簡體   English   中英

如何在Windows窗體應用程序中使用C#將Crystal Report連接到MS Access數據庫

[英]How to connect Crystal Report to MS Access database using C# in Windows Form Application

我想在Windows窗體應用程序中使用C#將Crystal Reports連接到MS Access數據庫。

我正在使用下面的代碼將Crystal Report與MS Access數據庫連接,但是它不起作用。

ReportDocument crReport = new ReportDocument(); 
string sTemplatePath = "D:\\Report\\Sample.rpt";

crReport.Load(sTemplatePath);

TableLogOnInfo crTableLogOnInfo = new TableLogOnInfo(); 
ConnectionInfo tConnInfo = new ConnectionInfo(); 
tConnInfo.DatabaseName = "D:\\AccessDB\\AccessDb.mdb"; 
tConnInfo.Password = "abcd"; 
foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crReport.Database.Tables) 
{             
    crTableLogOnInfo = crTable.LogOnInfo;
    crTableLogOnInfo.ConnectionInfo = tConnInfo;
    crTable.ApplyLogOnInfo(crTableLogOnInfo); 
}

誰能幫助我完成這項任務? 我會非常感謝你。

您需要采取的步驟:

  1. 創建您的.rpt文件。 為此,我總是使用一個單獨的庫來保存所有報告,但這不是必需的,您可以在主要解決方案中進行操作。 無論哪種方式,都必須確保您具有對CrustalDecisions.CrystalReports.Engine的引用; CrustalDecisions.ReportSource; CrustalDecisions.Shared; 和CrustalDecisions.Windows.Forms。
  2. 現在,您可以右鍵單擊項目,“添加”,“新建項目”,然后在Visual c#下單擊“ Crystal Reports”。 選擇您的報告名稱,單擊“添加”,接受“使用報告向導”和“標准”布局的默認設置。 現在,展開“創建新連接”,單擊“ Access / Excel”,系統將提示您輸入數據庫文件的位置。 由於已使用密碼保護了它,因此現在必須單擊“安全登錄”並輸入密碼(作為數據庫密碼)。 當您單擊“完成”時,您現在應該看到您的數據庫文件,並在其下是表的列表等。選擇一個作為報告基礎的文件,使用箭頭將其移至“選定的表”(右側)。 )。 單擊下一步,然后以相同的方式選擇所需的字段。 您現在可以單擊完成。
  3. 將CrystalReportViewer控件添加到窗體中,該控件將成為報表的“宿主”窗體。
  4. 使用與主機窗體的構造函數中相同的c#代碼連接數據庫。 只要它具有相同的表,這可能是具有不同密碼的其他數據庫。
  5. 最后添加以下代碼:

     //Assign data source details to the report viewer if (this.crystalReportViewer1.LogOnInfo != null) { TableLogOnInfos tlInfo = this.crystalReportViewer1.LogOnInfo; foreach (TableLogOnInfo tbloginfo in tlInfo) { tbloginfo.ConnectionInfo = tConnInfo; } } crystalReportViewer1.ReportSource = crReport; 

一旦您掌握了基礎知識,就可以使用所見即所得的設計器開始研究報告的設計!

暫無
暫無

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

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