RDLC报表
1.首先创建项目,并添加web页面,rdlc报表,以及dataset数据集
2.在数据集中创建数据表,并添加相应的字段。
3.设计报表样式:
3.1.打开rdlc报表,快捷键Ctrl+Alt+D打开报表数据,新建数据集
3.2 选择dataset建立好的数据表:
3.3 设计报表形式并拖入字段:
4.在web页面中添加reportview控件,并且一定要添加 <asp:ScriptManager runat=server></asp:ScriptManager>控件
5.编写后台代码:
在写后台代码之前必须引入必要的命名空间,using Microsoft.Reporting.WebForms;
此处还必须要注意引入报表的路径。并且代码必须放在 if (!IsPostBack)之内,否则报表会一直处于加载状态。
后台代码示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Data;
using MySql.Data.MySqlClient;
namespace RDLC练习
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable DataTableSql = new DataTable();
DataTableSql.Columns.Add("id");
DataTableSql.Columns.Add("bridgename");
DataTableSql.Columns.Add("longtitude");
DataTableSql.Columns.Add("latitude");
DataTableSql.Columns.Add("x_deflection");
DataTableSql.Columns.Add("y_deflection");
DataTableSql.Columns.Add("time_info"); //设计数据表结构;
string str = "select * from bridgemap_1 order by id asc LIMIT 1";
DataTableSql = GetData(str);
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet1";
rds.Value = DataTableSql; //---向报表绑定数据源
this.ReportViewer1.LocalReport.DataSources.Add(rds); //---向报表查看器指定显示的报表
this.ReportViewer1.LocalReport.ReportPath = @"D:\VS2013学习文件\RDLC练习\RDLC练习\Report1.rdlc";
}
}
public static DataTable GetData(string sqlString)
{
DataTable dt = new DataTable();
string constr = "Server=localhost;Data Source=127.0.0.1;User ID=root;Password=root;DataBase=bridgemap;Charset=utf8;"; //定义连接数据库的字符串,
MySqlConnection con = new MySqlConnection(constr); //建立连接
con.Open(); //打开链接
MySqlCommand cmd = new MySqlCommand(sqlString, con);//定义command对象,用于执行sql命令。(sqlString为要执行的sql语句,con为连接对象。)
MySqlDataReader dr = cmd.ExecuteReader(); //DataReader用于从查询的结果中读取数据。(dr为dataReader对象,为读取到的数据);
dt.Load(dr); //.Load方法:通过所提供的 DataReader,用某个数据源的值填充 DataTable。 如果 DataTable 已经包含行,则从数据源传入的数据与现有行合并。
dr.Close(); //关闭
return dt;
}
}
}
结果显示: