今天找VS2005 Beta2下载,偶然发现自己一年以前自己发的一个贴子,那个时候刚刚接触.net,就是做一个reporting service的项目,然后逐步从Java转到.net,现在看来自己已经进步很多.
研究了一下reportServices,还没有完全得其精髓,东拼西凑一个小例子如下:(实现搜索报表,保存报表,以及现实在form上),希望对初学者有些帮助。
***********************************************
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Web.Services.Protocols;
using System.Diagnostics;
namespace myApplication1
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
private myApplication1.ReportService.ReportingService myReportService =new myApplication1.ReportService.ReportingService();
private static myApplication1.ReportService.CatalogItem[] items = null;
private System.Windows.Forms.ComboBox comboBox1;
private AxSHDocVw.AxWebBrowser axWebBrowser1;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
// ReportService URL=http://localhost/ReportServer/ReportService.asmx?wsdl"%20filename="ReportService.wsdl
//
myApplication1.ReportService.ReportingService rs = new myApplication1.ReportService.ReportingService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
myApplication1.ReportService.SearchCondition condition = new myApplication1.ReportService.SearchCondition();
condition.Condition = myApplication1.ReportService.ConditionEnum.Contains;
condition.ConditionSpecified = true;
condition.Name = "Name";
condition.Value = "Report_A1";
myApplication1.ReportService.SearchCondition[] conditions = new myApplication1.ReportService.SearchCondition[1];
conditions[0] = condition;
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
items = rs.FindItems( "/", myApplication1.ReportService.BooleanOperatorEnum.Or, conditions );
if ( items != null )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
foreach ( myApplication1.ReportService.CatalogItem ci in items)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine( "Item {0} found at {1}", ci.Name, ci.Path );
comboBox1.Items.Add(ci.Name);
}
}
}
catch ( SoapException e )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine( e.Detail.InnerXml.ToString() );
}
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if( disposing )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (components != null)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(24, 32);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(200, 21);
this.comboBox1.TabIndex = 1;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// axWebBrowser1
//
this.axWebBrowser1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(32, 96);
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState"));
this.axWebBrowser1.Size = new System.Drawing.Size(576, 232);
this.axWebBrowser1.TabIndex = 2;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(648, 397);
this.Controls.Add(this.axWebBrowser1);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);
}
#endregion
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Application.Run(new Form1());
}
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myApplication1.ReportService.ReportingService rs = new myApplication1.ReportService.ReportingService();
rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Render arguments
byte[] result = null;
string reportPath = items[comboBox1.SelectedIndex].Path;
string format = "HTML4.0";
string historyID = null;
string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
// Prepare report parameter.
myApplication1.ReportService.ParameterValue[] parameters = null;
myApplication1.ReportService.DataSourceCredentials[] credentials = null;
string showHideToggle = null;
string encoding;
string mimeType;
myApplication1.ReportService.Warning[] warnings = null;
myApplication1.ReportService.ParameterValue[] reportHistoryParameters = null;
string[] streamIDs = null;
myApplication1.ReportService.SessionHeader sh =
new myApplication1.ReportService.SessionHeader();
rs.SessionHeaderValue = sh;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
result = rs.Render(reportPath, format, historyID, devInfo, parameters, credentials,
showHideToggle, out encoding, out mimeType, out reportHistoryParameters, out warnings,
out streamIDs);
sh.SessionId = rs.SessionHeaderValue.SessionId;
Console.WriteLine("SessionID after call to Render: {0}",
rs.SessionHeaderValue.SessionId);
Console.WriteLine("Execution date and time: {0}",
rs.SessionHeaderValue.ExecutionDateTime);
Console.WriteLine("Is new execution: {0}",
rs.SessionHeaderValue.IsNewExecution);
}
catch(SoapException err )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine(err.Detail.OuterXml);
}
// Write the contents of the report to an HTML file.
try
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
FileStream stream = File.Create( "report.htm", result.Length );
Console.WriteLine( "File created." );
stream.Write( result, 0, result.Length );
Console.WriteLine( "Result written to the file." );
stream.Close();
//axPdf1.src = "report.pdf";
int zero=0;
object oZero= zero;
string str = "";
object oStr = str;
axWebBrowser1.Navigate("http://localhost/ReportServer?%2freport+sample%2fReport_A1&rs:Command=Render",
ref oZero,
ref oStr,
ref oStr,
ref oStr);
}
catch ( Exception err )
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Console.WriteLine( err.Message );
}
}
}
}