繁体   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