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 个数据表在水晶报表中显示数据?


- 在 LINQ to SQL 中使用 contains() 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01