简体   繁体   English

如何在Windows窗体应用程序中使用C#将Crystal Report连接到MS Access数据库

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

I want to connect Crystal Reports to MS Access database using C# in my windows form application. 我想在Windows窗体应用程序中使用C#将Crystal Reports连接到MS Access数据库。

I am using below code to connect crystal report with MS Access database but it is not working. 我正在使用下面的代码将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); 
}

Can anyone help me to achieve this task? 谁能帮助我完成这项任务? I will be very thankful to you. 我会非常感谢你。

Steps you need to take: 您需要采取的步骤:

  1. Create your .rpt file. 创建您的.rpt文件。 For this I always use a separate library in which I keep all my reports, but this is not necessary, you can do it in your main solution. 为此,我总是使用一个单独的库来保存所有报告,但这不是必需的,您可以在主要解决方案中进行操作。 Either way, you must make sure you have references to CrustalDecisions.CrystalReports.Engine; 无论哪种方式,都必须确保您具有对CrustalDecisions.CrystalReports.Engine的引用; CrustalDecisions.ReportSource; CrustalDecisions.ReportSource; CrustalDecisions.Shared; CrustalDecisions.Shared; and CrustalDecisions.Windows.Forms. 和CrustalDecisions.Windows.Forms。
  2. You can now right-click your project, Add, New Item, and then under Visual c# you will see Crystal Reports. 现在,您可以右键单击项目,“添加”,“新建项目”,然后在Visual c#下单击“ Crystal Reports”。 Choose your report name, click Add, Accept the default of Using the Report Wizard and Standard layout. 选择您的报告名称,单击“添加”,接受“使用报告向导”和“标准”布局的默认设置。 Now expand Create New Connection, click Access/Excel and you will be prompted for the location of your database file. 现在,展开“创建新连接”,单击“ Access / Excel”,系统将提示您输入数据库文件的位置。 Since you have password protected it, you must now click Secure Logon and enter the password (as Database password). 由于已使用密码保护了它,因此现在必须单击“安全登录”并输入密码(作为数据库密码)。 When you click Finish, you should now see your database file and under it a list of tables etc. Select the one(s) that are the basis for your report, use the arrow to move it/them to Selected tables (right hand side). 当您单击“完成”时,您现在应该看到您的数据库文件,并在其下是表的列表等。选择一个作为报告基础的文件,使用箭头将其移至“选定的表”(右侧)。 )。 Click Next and then select the Fields that you need in the same way. 单击下一步,然后以相同的方式选择所需的字段。 You may now click Finish. 您现在可以单击完成。
  3. Add A CrystalReportViewer control to a Form which is to be the "host" form for the report. 将CrystalReportViewer控件添加到窗体中,该控件将成为报表的“宿主”窗体。
  4. Use the same c# code as you have in the constructor of your host form to connect to the database. 使用与主机窗体的构造函数中相同的c#代码连接数据库。 This may be a different database with a different password, as long as it has the same table(s). 只要它具有相同的表,这可能是具有不同密码的其他数据库。
  5. Finally add the following code: 最后添加以下代码:

     //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; 

Once you have got the basics working, you can start playing around with the design of your report, using the WYSIWYG designer! 一旦您掌握了基础知识,就可以使用所见即所得的设计器开始研究报告的设计!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用c#和ms-access和数据集时的Crystal Report数据库登录提示 - crystal report database login prompt when using c# and ms-access and dataset 如何在没有数据库的情况下生成水晶报表仅显示用户填写的表格数据在c#表单应用程序中 - how to generate crystal Report without Database Only for showing Form Data filled by User In c# form Application 如何在Windows窗体应用程序中通过C#删除Crystal报表中的重复标题 - How to remove duplicated title in crystal report via C# in windows form application 如何部署使用sql server express 2012和crystal report开发的C#windows表单应用程序 - how to deploye C# windows form application developed with sql server express 2012 and crystal report 如何在C#Windows应用程序中的Crystal Report中删除空白行 - How to remove blank lines in Crystal Report in c# Windows application 如何使用Crystal Report C#Windows Form获取详细信息部分中的记录计数? - How to get count of records in details section using crystal report c# windows form? 如何在Windows应用程序中使用C#打印Crystal报表而不显示预览和打印机选择 - How to Print Crystal Report Without Showing Preview and Printer Selection in Windows application using C# 是否可以直接将Crystal报表保存为pdf格式,而无需从C#Windows Form应用程序在客户端PC中安装Crystal报表? - Is it possible to directly save crystal report to pdf format without installing crystal report in client pc from C# windows form application? 如何使用数据库和Crystal Reports发布C#Windows应用程序? - How to publish a C# windows application with database and Crystal Reports? 使用MS-Access在Windows窗体应用程序C#中进行多个TextBox和ComboBox搜索 - Multiple TextBox and ComboBox Search in Windows Form Application C# using MS-Access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM