Reporting Service自动安装程序,

Reporting Service部署不是很方便,但可以通过程度来部署!

首先添加本地WebService的引用,
地址是: http://localhost/ReportServer/ReportService.asmx引用,可以把这个地址设置为动态方便以后修改。

创建对象:
private ReportingService rs = new ReportingService();
设置权限:
rs.Credentials = CredentialCache.DefaultCredentials;
创建文件夹:
rs.CreateFolder(tgParent, "/", null);
创建共享数据源:

DataSourceDefinition dsd = new DataSourceDefinition();
            dsd.CredentialRetrieval 
= CredentialRetrievalEnum.Store;
            dsd.ConnectString 
= "data source=" + ServerIP + ";initial catalog=RiskH2000";
            dsd.Enabled 
= true;
            dsd.EnabledSpecified 
= true;
            dsd.Extension 
= "SQL";
            dsd.ImpersonateUser 
= false;
            dsd.ImpersonateUserSpecified 
= true;
            dsd.Prompt 
= null;
            dsd.UserName 
= SqlUserName;
            dsd.Password 
= sqlPassword;
            dsd.WindowsCredentials 
= false;
            
try
            
{
                rs.CreateDataSource(name, parent, 
false, dsd, null);
            }

            
catch (Exception ex)
            
{
                WirteMsg(ex.Message);
            }

上传报表文件:
try
            
{
                FileStream stream 
= File.OpenRead(filePath + ReportName + ".rdl");
                definition 
= new byte[stream.Length];
                stream.Read(definition, 
0, (int) stream.Length);
                stream.Close();
            }

            
catch (IOException ex)
            
{
                WirteMsg(ex.Message);
            }

            warn 
= rs.CreateReport(ReportName, string.Format(@"/{0}",parent), true, definition, null);

            
if (warn != null)
            
{
                
foreach (Warning w in warn)
                    WirteMsg(w.Message);
            }

            
else
            
{
                WirteMsg(
string.Format("{0} 创建成功!", ReportName));
            }


全部代码文件如下:
using System;
using System.Configuration;
using System.IO;
using System.Net;
using ReportServiceSetup.RS;

namespace ReportServiceSetup
{
    
/// <summary>
    
/// Form1 的摘要说明。
    
/// </summary>

    public class DoSetting
    
{
        
private byte[] definition = null;
        
private ReportingService rs = new ReportingService();
        
private string tgParent = "";
        
private string jGParent = "";
        
private string headtgParent = "";
        
private string headJgparent = "";
        
private string ServerIP = "";
        
private string SqlUserName = "risk";
        
private string sqlPassword = "risk";
        
private string projctPath = @"D:\hg";
        
private string filePath = @"D:\HG\BIN\Reports\";
        
private Warning[] warn = null;
        DataSourceDefinition fDefinition;

        
public DoSetting()
        
{
            
//
            
// Windows 窗体设计器支持所必需的
            
//
            tgParent = GetSetting("TgParentName");
            headtgParent 
= GetSetting("HeadTgParentName");
            headJgparent 
=GetSetting("HeadJgParentName");
            jGParent 
= GetSetting("JgParentName");
            ServerIP 
= GetSetting("ServiceIP");
            SqlUserName 
= GetSetting("SqlUserName");
            sqlPassword 
= GetSetting("SqlUserPass");
            projctPath 
= GetSetting("ProjectPath");
            filePath 
= GetSetting("FilePath");
            
//
            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
            
//
        }


        
public string GetSetting(string Name)
        
{
            
return ConfigurationSettings.AppSettings[Name];
        }


        
public void DoIt()
        
{
            rs.Credentials 
= CredentialCache.DefaultCredentials;
            
try
            
{
                rs.CreateFolder(tgParent, 
"/"null);
            }

            
catch (Exception ex)
            
{
                WirteMsg(ex.Message);
            }

            
try
            
{
                rs.CreateFolder(jGParent, 
"/"null);
            }

            
catch (Exception ex)
            
{
                WirteMsg(ex.Message);
            }

            CreateDataSource(
"RiskSqlServerSource"string.Format(@"/{0}", tgParent));
            CreateDataSource2(
"RiskSource"string.Format(@"/{0}", tgParent));
            CreateDataSource(
"ParamConn"string.Format(@"/{0}", jGParent));
            CreateDataSource2(
"JGReport"string.Format(@"/{0}", jGParent));
            publishTG();
            PublicJG();
        }

        
public void DoIt2()
        
{
            rs.Credentials 
= CredentialCache.DefaultCredentials;
            
try
            
{
                rs.CreateFolder(headtgParent, 
"/"null);
            }

            
catch (Exception ex)
            
{
                WirteMsg(ex.Message);
            }

            
try
            
{
                rs.CreateFolder(headJgparent, 
"/"null);
            }

            
catch (Exception ex)
            
{
                WirteMsg(ex.Message);
            }

            CreateDataSource(
"RiskSqlServerSource"string.Format(@"/{0}", headtgParent));
            CreateDataSource(
"ParamConn"string.Format(@"/{0}", headJgparent));
            CreateDataSource2(
"JGReport"string.Format(@"/{0}", headJgparent));
            publishHead();

            
        }

        
private void publishHead()
        
{
            PublishReport(
"TG001",headtgParent);
            PublishReport(
"TG001_2",headtgParent);
            PublishReport(
"TG001_3",headtgParent);
            PublishReport(
"JG001_3",headJgparent);
            PublishReport(
"JG001_E",headJgparent);
            PublishReport(
"JG001_I",headJgparent);
            PublishReport(
"JG001_IE",headJgparent);
            PublishReport(
"JG001_2",headJgparent);
        }

        
private void publishTG()
        
{
            PublishReport(
"TG001",tgParent);
            PublishReport(
"TG001_2",tgParent);
            PublishReport(
"TG001_3",tgParent);
            PublishReport(
"Tg003",tgParent);
            PublishReport(
"Tg004",tgParent);
            PublishReport(
"TG005",tgParent);
            PublishReport(
"TG006",tgParent);
            PublishReport(
"TG006_2",tgParent);
            PublishReport(
"TG008",tgParent);
            PublishReport(
"TG009",tgParent);
            PublishReport(
"TG009_2",tgParent);
            PublishReport(
"TG009_3",tgParent);
            PublishReport(
"TG014",tgParent);
        }

        
public void PublicJG()
        
{
            PublishReport(
"JG023_2Report",jGParent);
            PublishReport(
"JG001_3",jGParent);
            PublishReport(
"JG001_E",jGParent);
            PublishReport(
"JG001_I",jGParent);
            PublishReport(
"JG001_IE",jGParent);
            PublishReport(
"JG004",jGParent);
            PublishReport(
"JG005",jGParent);
            PublishReport(
"JG023_1Report",jGParent);
            PublishReport(
"JG001_2",jGParent);
        }


        
public void CreateDataSource(string name, string parent)
        
{
            DataSourceDefinition dsd 
= new DataSourceDefinition();
            dsd.CredentialRetrieval 
= CredentialRetrievalEnum.Store;
            dsd.ConnectString 
= "data source=" + ServerIP + ";initial catalog=RiskH2000";
            dsd.Enabled 
= true;
            dsd.EnabledSpecified 
= true;
            dsd.Extension 
= "SQL";
            dsd.ImpersonateUser 
= false;
            dsd.ImpersonateUserSpecified 
= true;
            dsd.Prompt 
= null;
            dsd.UserName 
= SqlUserName;
            dsd.Password 
= sqlPassword;
            dsd.WindowsCredentials 
= false;
            
try
            
{
                rs.CreateDataSource(name, parent, 
false, dsd, null);
            }

            
catch (Exception ex)
            
{
                WirteMsg(ex.Message);
            }


        }


        
public void CreateDataSource2(string name, string parent)
        
{
             fDefinition 
= new DataSourceDefinition();
            fDefinition.CredentialRetrieval 
= CredentialRetrievalEnum.Integrated;
            fDefinition.ConnectString 
= @"<Config><ReportPath>" + projctPath + @"Web\Bin\</ReportPath><XmlPath>" + projctPath + @"Xml\</XmlPath><ConnectionString>USER ID=" + SqlUserName + ";PASSWORD=" + sqlPassword + ";INITIAL CATALOG=RiskH2000; DATA SOURCE=" + ServerIP + ";CONNECT TIMEOUT=30</ConnectionString></Config>";
            fDefinition.Enabled 
= true;
            fDefinition.EnabledSpecified 
= true;
            fDefinition.Extension 
= "YJC.Toolkit";
            fDefinition.ImpersonateUser 
= false;
            fDefinition.ImpersonateUserSpecified 
= true;
            fDefinition.Prompt 
= null;
            fDefinition.WindowsCredentials 
= true;
            
try
            
{
                rs.CreateDataSource(name, parent, 
false, fDefinition, null);
            }

            
catch (Exception ex)
            
{
                WirteMsg(ex.Message);
            }

        }


        
public void WirteMsg(string msg)
        
{
            Console.WriteLine(msg);
        }


        
public void PublishReport(string ReportName,string parent)
        
{
            
try
            
{
                FileStream stream 
= File.OpenRead(filePath + ReportName + ".rdl");
                definition 
= new byte[stream.Length];
                stream.Read(definition, 
0, (int) stream.Length);
                stream.Close();
            }

            
catch (IOException ex)
            
{
                WirteMsg(ex.Message);
            }

            warn 
= rs.CreateReport(ReportName, string.Format(@"/{0}",parent), true, definition, null);

            
if (warn != null)
            
{
                
foreach (Warning w in warn)
                    WirteMsg(w.Message);
            }

            
else
            
{
                WirteMsg(
string.Format("{0} 创建成功!", ReportName));
            }


        }


    }

}
posted on 2005-07-30 12:40  edobnet  阅读(3049)  评论(5编辑  收藏  举报