RDLC报表---自定义数据集

本文介绍如何使用C#进行SQL数据查询,并通过RDLC报表的形式展示查询结果。主要内容包括:设置SQL连接字符串、执行SQL命令、使用SqlDataAdapter填充DataSet、配置及显示RDLC报表。
//连接字符串
string connstr = "Data Source=.;Initial Catalog=WWALMDB;User ID=sa;Password=sqlsql";

//新建连接
SqlConnection connection = new SqlConnection(connstr);

//查询命令
SqlCommand command = new SqlCommand(textBoxSQL.Text, connection);

//适配器
SqlDataAdapter adapter = new SqlDataAdapter(command);

//数据集
DataSet ds = new DataSet();

//查询并填充
try
{
connection.Open();
adapter.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
connection.Close();
command.Dispose();
connection.Dispose();
}

//指定rdlc报表
this.reportViewer1.LocalReport.ReportEmbeddedResource = "FormReport.EventReport.rdlc";

//清空reportviewer先前的数据源
this.reportViewer1.LocalReport.DataSources.Clear();

//新建数据源
ReportDataSource rs = new ReportDataSource();
rs.Name
= "EventHistory";//"EventHistory"是rdlc报表Tablib属性下的数据集名称
rs.Value = ds.Tables[0];

//添加数据源
this.reportViewer1.LocalReport.DataSources.Add(rs);

//显示数据
this.reportViewer1.RefreshReport();

 

转载于:https://www.cnblogs.com/jiewei915/archive/2010/11/24/1886536.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值