Reporting Service

研究了一下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
{
    
/// <summary>
    
/// Summary description for Form1.
    
/// </summary>

    public class Form1 : System.Windows.Forms.Form
    
{
        
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;

        
/// <summary>
        
/// Required designer variable.
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
public Form1()
        
{
            
//
            
// 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
            
{
                items 
= rs.FindItems( "/", myApplication1.ReportService.BooleanOperatorEnum.Or, conditions );
                
if ( items != null )
                
{
                    
foreach ( myApplication1.ReportService.CatalogItem ci in items)
                    
{
                        Console.WriteLine( 
"Item {0} found at {1}", ci.Name, ci.Path );
                        comboBox1.Items.Add(ci.Name);
                    }

                }

            }

            
catch ( SoapException e )
            
{
                Console.WriteLine( e.Detail.InnerXml.ToString() ); 
            }

        }


        
/// <summary>
        
/// Clean up any resources being used.
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if (components != null
                
{
                    components.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
#region Windows Form Designer generated code
        
/// <summary>
        
/// Required method for Designer support - do not modify
        
/// the contents of this method with the code editor.
        
/// </summary>

        private void InitializeComponent()
        
{
            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(2432);
            
this.comboBox1.Name = "comboBox1";
            
this.comboBox1.Size = new System.Drawing.Size(20021);
            
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(3296);
            
this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState"));
            
this.axWebBrowser1.Size = new System.Drawing.Size(576232);
            
this.axWebBrowser1.TabIndex = 2;
            
// 
            
// Form1
            
// 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            
this.ClientSize = new System.Drawing.Size(648397);
            
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


        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main() 
        
{
            Application.Run(
new Form1());
        }



        
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        
{            
            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;

            
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 )
            
{
                Console.WriteLine(err.Detail.OuterXml);
            }

            
// Write the contents of the report to an HTML file.
            try
            
{
                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 )
            
{
                Console.WriteLine( err.Message );
            }


        }

    }

}

posted @ 2005-04-26 10:33  让心灵去旅行  阅读(1521)  评论(1编辑  收藏  举报