RDLC 报表开发

1. 刚才的SQL 脚本

CREATE PROC  EmployeeReport

AS

SELECT  *  FROM Employee 

GO

实际的报表开发中,一定不要用SELECT * ,只取报表中需要查看的字段。

2. 有时候,可能需要用户选择一些条件,有选择性的查看报表。而不是全部绑定数据

如上图,用户可能只需要查看2008-9-29至2008-9-30时间段之间的数据

则作法如下

先建立好如上图的ASPX页面,在View Report 事件中写如下的程序

ReportViewer1.LocalReport.ReportPath = AppDomain.CurrentDomain.BaseDirectory + "/Report/Request.rdlc";

DateTime dtFrom =Convert.ToDateTime(txtDateFrom.Text);

DateTime dtTo =Convert.ToDateTime(txtDateTo.Text);

string requester = txtRequester.Text;

string dept = txtRequestDept.Text;

string material = ddlMaterial.SelectedValue;       

string iprstatus = ddlStatus.SelectedValue;

DataTable reqrpt = ReportDB.RequestReport(dtFrom, dtTo, material, dept,requester, iprstatus);

if (reqrpt != null)

      {

           ReportViewer1.LocalReport.DataSources.Clear();

            ReportViewer1.LocalReport.DataSources.Add(

                new Microsoft.Reporting.WebForms.ReportDataSource("Request_RequestReport", reqrpt));

            ReportViewer1.LocalReport.Refresh();

        }      

ReportViewer1.LocalReport.Refresh();

根据用户所选的参数,把数据值传到SQL语句中即可.下面是RequestReport方法的源码

DataTable RequestReport(DateTime dtFrom, DateTime dtTo, string pMaterial, string pDept, string pRequester, string pIPRStatus)        {

            string MySQL = Purchase;

            string whDate = " RequestDate BETWEEN '{0}'  AND '{1}'  ";

            MySQL = MySQL + string.Format(whDate, dtFrom, dtTo);

            string whMaterial = "  AND  MaterialCode='{0}' ";

            if (pMaterial != "ALL")

            {

                MySQL = MySQL + string.Format(whMaterial, pMaterial);

            }

            string whDept = " AND  RequestDepartment='{0}' ";

            MySQL = MySQL + string.Format(whDept, pDept);

           string  whRequester=" AND  Requester='{0}' ";

            if(pRequester!="ALL")

               MySQL = MySQL + string.Format(whRequester, pRequester);

            string whIPRStatus = " AND IPRStatus={0}  ";         

            if (pIPRStatus != "ALL")

            {

                MySQL = MySQL + string.Format(whIPRStatus, pIPRStatus);

            }        

            IDataProvider privider = DataProvider.CreateDataProvider();

            DataSet ds = privider.RetriveDataSet(MySQL);

            if (ds != null && ds.Tables.Count > 0)

                return ds.Tables[0];

            else

                return null;

}

const string Purchase="SELECT Name FROM Employee";

                         

3. 设计报表时,可以用上述的方法,实际运行时,可以替换成SQL 语句,传到ReportDataSource中即可,只要相当的表结构字段是存在的。

4. 报表的定义是XML结构的,如果熟悉报表的定义格式规范,可以用文本编辑器打开直接修改。

5. 如果采用SQL Server 2005的服务器端报表,可能还会有进一步的方便设计和开发的地方.这里采用的是.net framework的组件,客户端只需要安装.net framework2.0即可,无需安装额外的组件.

原文地址:https://www.cnblogs.com/dlf-myDream/p/4682255.html