ASP.NET中访问SQL 2005报表服务(一)

来源:互联网 发布:c语言产生1到9的随机数 编辑:程序博客网 时间:2024/05/16 16:59

通过ReportView控件访问SQL 2005报表服务

 

首先,安装ReportView控件。如果使用的是SQL 2000报表服务器,到这个目录去找到源码工程 C:/Program Files/Microsoft SQL Server/MSSQL/Reporting Services/Samples/Applications/ReportViewer/vb and C:/Program Files/Microsoft SQL Server/MSSQL/Reporting Services/Samples/Applications/ReportViewer /cs,然后编译。

 

如果直接使用SQL 2005的报表服务器,只要安装时选择了work station,就会自动安装上这个控件。不需要自己编译了。

 

ASP.NET工程中,新建一个web page,加入一个ReportView控件ReportViewer1。如果工具箱里没有,则需要自己添加。

 

修改ReportViewer1ReportServerUrlReportPath两个属性:

ReportServerUrl=http:// ReportServerName/reportserver (ReportServerName报表服务器的名字)

ReportPath=/Demo/Report1 (/Demo/Report1是你的报表所在路径,注意最前面的/)

完成代码如下

            //即报表服务器的URL地址

            this.ReportViewer1.ServerReport.ReportServerUrl = new Uri("http:// ReportServerName //reportserver");

            //ReportFolderReportName的组合

            this.ReportViewer1.ServerReport.ReportPath = "/Demo/Report1";

 

运行程序,在ReportView1的位置出现了报表,和从URL访问一模一样。

 

如果是有参数输入的报表,又希望在页面自定义参数输入。比如报表里有两个时间参数,开始时间和结束时间。如果直接在文本输入框输入2007-1-1,非常不方便。可以在web page上加一个日期选择的控件来代替直接输入日期。

 

这需要两步:

 

1 ShowParameterPrompts设置为false. 即关闭报表服务器提供的参数输入区域。

this.ReportViewer1.ShowParameterPrompts = false;

 

2 web page上增加START DATEEND DATE两个日期控件,和一个VIREW REPORT的按钮ButtonViewReport

ButtonViewReport按钮的CLICK事件中,将日期控件的值用SetParameters方法传递给服务器。

    protected void ButtonViewReport_Click(object sender, EventArgs e)

    {
         DateTime StartDate = System.Convert.ToDateTime(TextBoxStartDate.Value);
         DateTime EndDate = System.Convert.ToDateTime(TextBoxEndDate.Value);
        
//注意ReportParameter的命名空间
        
Microsoft.Reporting.WebForms.ReportParameter[] Parameters = new Microsoft.Reporting.WebForms.ReportParameter[2];    

        //这里的StartDateEndDate必须和报表服务里定义的参数名相同
         Parameters[0] =
new ReportParameter("StartDate", StartDate.ToShortDateString());    
         Parameters[1] =
new ReportParameter("EndDate
", EndDate.ToShortDateString());
         ReportViewer1.ServerReport.SetParameters(Parameters);
     }

 

原创粉丝点击