ASP.NET中訪問SQL 2005報表服務(一)

通過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);
     }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章