使用该控件能很方便做文字报表 如图
报表的列标题可以直接在界面设计,字体字体颜色等属性;直接支持导出到excel,pdf等文件,直接支持打印功能,无需另外写代码实现;
可生成曲线
核心代码如下(自定义查询)
string MyConn = "server=" + "127.0.0.1" + ";uid=" + "sa" + ";pwd=" + "root"
+ ";database=" + "student" + ";Trusted_Connection=no";
SqlConnection MyConnection = new SqlConnection(MyConn);
//string strSql = "select * from student where stuname ='test1'";
string strSql = "select * from student ";
if (textBox1.Text.Trim().Length > 0)
strSql = "select * from student where stuname='" + textBox1.Text.Trim() + "'";
SqlCommand MyCommand = new SqlCommand();
SqlDataReader mySqlDataReader;
//DataSet MyDataSet = new DataSetNew();//把已创建好的数据源赋值给数据集变量
DataSet MyDataSet = new studentDataSet();//把已创建好的数据源赋值给数据集变量 studentDataSet
try
{
//打开连接并执行sql语句
MyConnection.Open();
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
MyCommand.CommandText = strSql;
mySqlDataReader = MyCommand.ExecuteReader(); //执行sql语句,把读取的结果给SqlDataReader类的实例
//把读取来的数据加载到数据集中。
MyDataSet.Tables[0].Load(mySqlDataReader);
//关闭读取及其连接
mySqlDataReader.Close();
MyConnection.Close();
//为查看器提供本地报表数据
reportViewer1.LocalReport.ReportPath = "Report1.rdlc";//把Report2.rdlc 文件放在bin目录下。
reportViewer1.LocalReport.ReportEmbeddedResource = "Report1.rdlc";//获取或设置报表嵌入资源的名称
//准备报表数据源
ReportDataSource rds = new ReportDataSource();
rds.Name = "DataSet1";//这个名字别错了
int t = MyDataSet.Tables.Count;
rds.Value = MyDataSet.Tables[0];
reportViewer1.LocalReport.DataSources.Clear();
reportViewer1.LocalReport.DataSources.Add(rds);//把数据源加载到reportview中去
reportViewer1.RefreshReport();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (MyConnection.State == ConnectionState.Open)
{
MyConnection.Close();
}
}