How to display data in crystal report using 2 sql request and 2 datatables in dataset?(如何使用数据集中的 2 个 sql 请求和 2 个数据表在水晶报表中显示数据?)
问题描述
我有一个包含 2 个数据表的数据集,我需要使用 2 个 sql 请求在水晶报表中显示数据.所以我在我的数据集中创建了 2 个数据表(DataTable1 和 dataTable2)我尝试了这段代码,但它总是执行第二个 sql 请求!!
I have a dataset with 2 datatable aand I need to use 2 sql request to display data in crystal report. So I create 2 datatable in my dataset (DataTable1 and dataTable2) I tried this code but it always execute the second sql request!!
con.ConnectionString = @"connection";
string sql = "MyRequest1";
string sql1 = "MyRequest2";
DataSet1 ds = new DataSet1();
SqlDataAdapter dad = new SqlDataAdapter(sql, con);
SqlDataAdapter dad1 = new SqlDataAdapter(sql1, con);
dad.Fill(ds.Tables["DataTable1"]);
dad1.Fill(ds.Tables["DataTable2"]);
CrystalReport1 report = new CrystalReport1();
report.SetDataSource(ds.Tables["DataTable2"]);
report.SetDataSource(ds.Tables["DataTable1"]);
crystalReportViewer1.ReportSource = report;
crystalReportViewer1.Refresh();
推荐答案
解决方案是为Dataset中使用的每个DataTable实现一个Datatable方法:以第一个Datatable为例:
the solution is to implement a Datatable Method for each DataTable used in the Dataset: example for the 1st Datatable:
protected DataTable DataTable1()
{
string sql = "MyRequest";
SqlDataAdapter dad = new SqlDataAdapter(sql, con);
DataSet1 ds = new DataSet1();
dad.Fill(ds.Tables["NameOfDataTable"]);
DataTable dt = ds.Tables["NameOfDataTable"];
return dt;
}
并在打印按钮中添加以下代码:
and in the print button you add this code:
try {
DataSet ds = new DataSet();
DataTable dt1 = DataTable1().Copy(); //the name of the method
ds.Tables.Add(dt1);
CrystalReport1 myreport = new CrystalReport1();
myreport.SetDataSource(ds);
crystalReportViewer1.ReportSource = myreport;
}
catch (Exception ex)
{
//code ...
}
成功了:)
这篇关于如何使用数据集中的 2 个 sql 请求和 2 个数据表在水晶报表中显示数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用数据集中的 2 个 sql 请求和 2 个数据表在水晶报表中显示数据?


基础教程推荐
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01