2008-12-15 24 views
14

Tôi đang tìm kiếm một chút trợ giúp về các tham số truyền theo chương trình cho báo cáo SSRS qua VB.NET và ASP.NET. Điều này có vẻ như nó phải là một điều tương đối đơn giản để làm, nhưng tôi đã không có nhiều may mắn tìm kiếm sự giúp đỡ về điều này.Cách chuyển các tham số tới báo cáo SSRS theo chương trình

Có ai có bất kỳ đề xuất nào về nơi cần đến để nhận trợ giúp về điều này hay thậm chí một số mã mẫu?

Cảm ơn.

Trả lời

15

Bạn có thể làm như sau ,: (nó hoạt động cả trên báo cáo cục bộ như trong báo cáo SSRS toàn diện. nhưng trong chế độ đầy đủ, sử dụng lớp thích hợp, phần thông số vẫn giữ nguyên)

LocalReport myReport = new LocalReport(); 
myReport.ReportPath = Server.MapPath("~/Path/To/Report.rdlc"); 

ReportParameter myParam = new ReportParameter("ParamName", "ParamValue"); 
myReport.SetParameters(new ReportParameter[] { myParam }); 

// more code here to render report 
+1

nếu im không sử dụng báo cáo địa phương nhưng không muốn để lại giá trị rõ ràng trên url? – Leonardo

11

Nếu máy chủ Báo cáo trực tiếp truy cập, bạn có thể vượt qua các thông số trong chuỗi truy vấn nếu bạn đang truy cập vào repoort với một URL:

http://MyServer/ReportServer/?MyReport&rs:Command=Render&Param1=54321&Param2=product

Bạn có thể thêm định dạng đầu ra bằng cách thêm những điều sau ở cuối dòng URL:

& rs: Format = Excel

hoặc

& rs: Định dạng = PDF

+1

Chuyển các tham số dưới dạng URL có vẻ như mở ra cánh cửa cho người nào đó để hack báo cáo của bạn. Điều này có thể không quan trọng đối với một trang nội bộ, nhưng có thể không phải cho trang web công khai. – LunaCrescens

1

Nó được một lúc kể từ khi tôi đã làm mã này, nhưng nó có thể giúp: dự án web của bạn có phải là một trang web, và không phải là một dự án kiểu "ASP.Net Web Application", hoặc bạn sẽ không thể thêm tham chiếu được đề cập dưới đây. Nhấp chuột phải vào dự án và thêm một thư mục ASP.Net - App_WebReferences. Bạn sẽ phải chỉ định máy chủ nơi SRS của bạn là; chọn .asmx. Sau khi được thêm, thư mục dưới cấp đó được gọi là RSService và dưới đây là 2 điều: reportservice.discomap & .wsdl. Trong VB của tôi, tôi làm Imports RSService và nhập khẩu System.Web.Services.Protocols, sau đó ...

Dim MyRS As New ReportingService 

Dịch vụ báo cáo trên một máy chủ khác với máy chủ web ứng dụng được bật, vì vậy tôi có thể' t làm như sau: MyRS.Credentials = System.Net.CredentialCache.DefaultCredentials

Thay vào đó:. MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3), nơi RS1/2/3 là đăng nhập để SRS hộp, mật khẩu để SRS hộp, & tên miền"(. đây là những mã hóa trong web.config của tôi)

Sau đó, khối lượng dán:

MyRS.Credentials = New System.Net.NetworkCredential(rs1, rs2, rs3) 

Dim ReportByteArray As Byte() = Nothing 
Dim ReportPath As String = "/SRSSiteSubFolder/ReportNameWithoutRDLExtension" 
Dim ReportFormat As String = "PDF" 
Dim HistoryID As String = Nothing 
Dim DevInfo As String = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>" 
'Dim x As ReportParameter - not necessary 
Dim ReportParams(0) As ParameterValue 
ReportParams(0) = New ParameterValue() 
ReportParams(0).Name = "TheParamName" 
ReportParams(0).Value = WhateverValue 

Dim Credentials As DataSourceCredentials() = Nothing 
Dim ShowHideToggle As String = Nothing 
Dim Encoding As String 
Dim MimeType As String 
Dim ReportHistoryParameters As ParameterValue() = Nothing 
Dim Warnings As Warning() = Nothing 
Dim StreamIDs As String() = Nothing 
'Dim sh As New SessionHeader() - not necessary 
''MyRS.SessionHeaderValue = sh - not necessary 

ReportByteArray = MyRS.Render(ReportPath, ReportFormat, HistoryID, DevInfo, ReportParams, Credentials, _ 
    ShowHideToggle, Encoding, MimeType, ReportHistoryParameters, Warnings, StreamIDs) 
'(Yay! That line was giving "HTTP error 401 - Unauthorized", until I set the credentials 
' as above, as explained by http://www.odetocode.com/Articles/216.aspx.) 

'Write the contents of the report to a PDF file: 
Dim fs As FileStream = File.Create(FullReportPath, ReportByteArray.Length) 
fs.Write(ReportByteArray, 0, ReportByteArray.Length) 
fs.Close() 

Call EmailTheReport(FullReportPath) 

If IO.File.Exists(FullReportPath) Then 
    IO.File.Delete(FullReportPath) 
End If 
2
ReportViewer1.LocalReport.DataSources.Clear(); 
ReportViewer1.Reset(); 
Label1.Visible = false; 
ReportViewer1.Visible = true; 
DataSet dataSet = new DataSet(); 
dataSet = new ClassBLL().Load_Report_Detail(TextBox1.Text, 
ddlType.SelectedValue, levelcode, fields); 
ReportDataSource datasource = new ReportDataSource("DataSet_StoreprocedureName", 
dataSet.Tables[0]); 

if (dataSet.Tables[0].Rows.Count == 0) 
{ 
    ReportViewer1.Visible = false; 
} 

ReportViewer1.LocalReport.ReportPath = Server.MapPath("") + @"\Report.rdlc"; 
ReportViewer1.LocalReport.DataSources.Clear(); 
ReportViewer1.LocalReport.DataSources.Add(datasource); 
string fields="name,girish,Z0117"; 
string[] filedName = fields.Split(','); 
ReportParameter[] param = new ReportParameter[2]; 

//for (int i = 0; i < filedName.Length; i++) 
//{ 

param[0] = new ReportParameter(filedName[0], filedName[0], true); 
param[1] = new ReportParameter(filedName[3], filedName[3], true); 

// } 


ReportViewer1.LocalReport.SetParameters(param); 

ReportViewer1.ServerReport.Refresh(); 
+1

Vui lòng thêm một số nhận xét vào mã của bạn. –

Các vấn đề liên quan