How to connect SQL Server Compact Edition database to Crystal Report in C#

懵懂的女人 提交于 2019-11-28 13:04:08
Alex Jolig

So I found my solution thanks to this helpful CodeProject sample

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

  1. Create a Winform and add a button and a CrystalReportViewer control to it.

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

  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.

  2. 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. Click finish and your columns will automatically be added in CrystalReport.

  2. 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;
    }
    
  3. Enjoy your report :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!