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

后端 未结 1 460
情歌与酒
情歌与酒 2020-12-11 13:12

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 othe

相关标签:
1条回答
  • 2020-12-11 13:48

    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.

    enter image description here

    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.

    enter image description here

    enter image description here

    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 :)

    0 讨论(0)
提交回复
热议问题