如何在 C# 中将 SQL Server Compact Edition 数据库连接到 Crystal Report [英] How to connect SQL Server Compact Edition database to Crystal Report in C#

查看:17
本文介绍了如何在 C# 中将 SQL Server Compact Edition 数据库连接到 Crystal Report的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的 SQL Server Compact Edition 数据库连接到 Crystal Report.我整天都在搜索,到目前为止,我在其他网站上发现了很多与之相关的问题,但没有一个有效的答案.

I'm trying to connect my SQL Server Compact Edition database to Crystal Report. I've been searching all day long and I've found many question related to it so far on other websites, but none had a working answer.

我还发现 这个问题在这个网站上要求 VB.Net 解决方案.还有这个链接互联网,并且有一种令人困惑的方式通过 XML 来实现,我无法弄清楚.

I also found this question in this site which has asked for VB.Net solution. And this link which is all over the internet and has a confusing way to do it by XML, which I couldn't figure it out.

SQL Server 和 Access 中的其他解决方案链接没有帮助.

There are other links with solutions in SQL Server and Access which don't help.

我想知道是否有一种简单的方法可以将 SQL Server CE 数据库连接到真正有效的 Crystal Report?

I'm wondering is there a simple way to connect a SQL Server CE database to Crystal Report which actually works?

推荐答案

感谢这个有用的代码项目示例

我将演示一个更简单的示例,以便更容易理解.

I will demonstrate an easier sample to make it easier to figure it out.

  1. 创建一个 Winform 并向其添加一个按钮和一个 CrystalReportViewer 控件.

使用解决方案资源管理器中的 add -> New Items 将数据集(*.xsd 文件)添加到您的项目.之后,将 DataTable 添加到 DataSet.

Add a DataSet (*.xsd file) to your project using add -> New Items in solution explorer. After that, add a DataTable to the DataSet.

  1. 将列添加到 DataTable.最好将它们命名为与您要在报告中显示的列相同的名称.列数取决于 Crystal 报表中应显示的列数.

  1. Add columns to DataTable. It's better to name them the same as the columns you are going to display on your report. The number of columns depends on how many columns should be displayed in the Crystal report.

使用add -> New Items 将Crystal Report 添加到项目中,使用报表向导,选择Project 数据源的ADO.NET DataSets 作为Crystal Report 的数据源,并选择你刚刚的数据表在您的 DataSet 中创建,作为 Crystal Report 的选定表.

Add a Crystal Report into the project using add -> New Items and using the Report Wizard, choose ADO.NET DataSets of the Project data source as the data source of the Crystal Report and select the data table you just created in your DataSet, as the selected table of the Crystal Report.

  1. 点击完成,您的列将自动添加到CrystalReport中.

进入按钮点击事件,在里面写下这些代码.

Go to the button click event and write these codes in it.

private void btnGo_Click(object sender, EventArgs e)
{
    CrReport2 objRpt = new CrReport2();
    string query = "Select Name,Number from tblInfo";  //Your sql query
    SqlCeConnection conn =
        new SqlCeConnection(
           @"Data Source=|DataDirectory|myDB.sdf;Persist Security Info=False"); //Your connection

    SqlCeDataAdapter adepter = new SqlCeDataAdapter(query, conn);
    DsReport Ds = new DsReport(); //DsReport is my dataset

    adepter.Fill(Ds, "customer"); //customer is my datatable in dataset

    objRpt.SetDataSource(Ds);
    crystalReportViewer1.ReportSource = objRpt;
}

  • 享受你的报告:)

  • Enjoy your report :)

    这篇关于如何在 C# 中将 SQL Server Compact Edition 数据库连接到 Crystal Report的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆