コード例 #1
0
        private void Enumerate2005Server(SSRS2005.ReportingService2005 reportingServer2005, String path, bool recursive)
        {
            byte[] reportDefinition = null;
            String commandText      = string.Empty;
            String dsName           = string.Empty;
            String sdsName          = string.Empty;
            int    reportID         = -1;
            int    dsID             = -1;

            SSRS2005.DataSourceDefinition dsDef;
            SSRS2005.DataSource[]         dsArray;
            Dictionary <String, int>      dsNameToRepository = new Dictionary <string, int>();

            // Get the array of items that are in the location that has been requested.
            SSRS2005.CatalogItem[] items = reportingServer2005.ListChildren(path, recursive);

            // Step through the items.
            foreach (SSRS2005.CatalogItem item in items)
            {
                dsNameToRepository.Clear();

                #region item.TypeName = Report
                if (item.Type == SSRS2005.ItemTypeEnum.Report)
                {
                    Console.WriteLine(string.Format("Analysing Report {0}", item.Name));
                    reportID = repository.AddObject(item.Path, item.Name, ReportEnumerator.ObjectTypes.Report, reportServerID);

                    reportDefinition = reportingServer2005.GetReportDefinition(item.Path);
                    dsArray          = reportingServer2005.GetItemDataSources(item.Path);
                    foreach (SSRS2005.DataSource ds in dsArray)
                    {
                        if (ds.Item is SSRS2005.DataSourceReference)
                        {
                            dsNameToRepository.Add(ds.Name, HandleDataSource(reportingServer2005, ((SSRS2005.DataSourceReference)ds.Item).Reference));
                        }
                        else if (ds.Item is SSRS2005.DataSourceDefinition)
                        {
                            dsNameToRepository.Add(ds.Name, HandleDataSource(((SSRS2005.DataSourceDefinition)ds.Item).ConnectString, ds.Name));
                        }
                    }

                    using (var stream = new MemoryStream(reportDefinition))
                    {
                        using (var xmlreader = new XmlTextReader(stream))
                        {
                            xmlreader.MoveToContent();
                            if (xmlreader.ReadToDescendant("DataSet"))
                            {
                                do
                                {
                                    XmlReader dsReader = xmlreader.ReadSubtree();
                                    dsReader.MoveToContent();
                                    if (dsReader.IsStartElement("DataSet"))
                                    {
                                        dsReader.ReadStartElement();
                                        dsReader.MoveToContent();
                                    }
                                    if (dsReader.IsStartElement("Query"))
                                    {
                                        while (dsReader.ReadToDescendant("DataSourceName"))
                                        {
                                            dsName = dsReader.ReadString();
                                            dsID   = -1;
                                            if (!dsNameToRepository.TryGetValue(dsName, out dsID))
                                            {
                                                Console.WriteLine(string.Format("Unable to locate DataSourceName {0} Using First Value", dsName));
                                                dsID = dsNameToRepository.ElementAt(0).Value;
                                            }
                                        }
                                        while (dsReader.ReadToNextSibling("CommandText"))
                                        {
                                            commandText = dsReader.ReadString();
                                        }
                                        if (dsID != -1)
                                        {
                                            ParseTSqlStatement(commandText, dsID, reportID);
                                        }
                                    }
                                    else if (dsReader.IsStartElement("Fields"))
                                    {
                                        dsReader.ReadInnerXml();
                                        if (dsReader.IsStartElement("Query"))
                                        {
                                            while (dsReader.ReadToDescendant("DataSourceName"))
                                            {
                                                dsName = dsReader.ReadString();
                                                dsID   = -1;
                                                if (!dsNameToRepository.TryGetValue(dsName, out dsID))
                                                {
                                                    Console.WriteLine(string.Format("Unable to locate DataSourceName {0} Using First Value", dsName));
                                                    dsID = dsNameToRepository.ElementAt(0).Value;
                                                }
                                            }
                                            while (dsReader.ReadToNextSibling("CommandText"))
                                            {
                                                commandText = dsReader.ReadString();
                                            }
                                            if (dsID != -1)
                                            {
                                                ParseTSqlStatement(commandText, dsID, reportID);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        while (dsReader.ReadToDescendant("SharedDataSetReference"))
                                        {
                                            sdsName = dsReader.ReadString();
                                            dsID    = repository.GetDataSet(sdsName, reportServerID);
                                            if (!repository.DoesMappingExist(dsID, reportID))
                                            {
                                                repository.AddMapping(dsID, reportID);
                                            }
                                            dsID = -1;
                                        }
                                    }
                                }while (xmlreader.ReadToFollowing("DataSet"));
                            }
                        }
                    }
                }
                #endregion
                else if (item.Type == SSRS2005.ItemTypeEnum.DataSource)
                {
                    HandleDataSource(reportingServer2005, item.Path);
                }
            }
        }
コード例 #2
0
        public void EnumerateReportingServer(string reportingServerURL, bool recursive, bool storeThreePartNames)
        {
            Boolean isRS2010 = true, isRS2005 = false;
            Uri     reportingServerUri = new Uri(reportingServerURL);
            string  decodedURL, decodedPath;

            SSRS2010.CatalogItem[]        items2010 = null;
            SSRS2010.ReportingService2010 reportingServer2010 = new SSRS2010.ReportingService2010();

            threePartNames = storeThreePartNames;
            if (reportingServerUri.IsDefaultPort)
            {
                decodedURL = reportingServerUri.Scheme + "://" + reportingServerUri.Host + reportingServerUri.LocalPath;
            }
            else
            {
                decodedURL = reportingServerUri.Scheme + "://" + reportingServerUri.Host + ":" + reportingServerUri.Port.ToString() + reportingServerUri.LocalPath;
            }
            decodedPath = Uri.UnescapeDataString(reportingServerUri.Query.Substring(1));

            reportingServer2010.Credentials = System.Net.CredentialCache.DefaultCredentials;
            reportingServer2010.Url         = decodedURL + @"/ReportService2010.asmx";

            // Assign the Report Server ID, so that all subsequent objects can be related to this.
            reportServerID = repository.AddObject(reportingServerUri.Host, decodedURL, ReportEnumerator.ObjectTypes.ReportServer, repository.RootRepositoryObjectID);

            SSRS2005.CatalogItem[]        items2005 = null;
            SSRS2005.ReportingService2005 reportingServer2005 = new SSRS2005.ReportingService2005();
            reportingServer2005.Credentials = System.Net.CredentialCache.DefaultCredentials;
            reportingServer2005.Url         = decodedURL + @"/ReportService2005.asmx";

            datasourceInRepository = new Dictionary <string, int>();

            // Try to connect to the Reporting Server as SQL 2008 R2 first.
            try
            {
                items2010 = reportingServer2010.ListChildren(decodedPath, false);
            }
            catch
            {
                isRS2010 = false;
            }


            if (!isRS2010)
            {
                try
                {
                    items2005 = reportingServer2005.ListChildren(decodedPath, false);
                    isRS2005  = true;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(string.Format("Unable to connect to either SQL2008R2 or SQL2005 services\r\nMessage {0}\r\nStack Trace {1}", ex.Message, ex.StackTrace));
                    return;
                }
            }
            else
            {
                Enumerate2010Server(reportingServer2010, decodedPath, recursive);
            }
            if (isRS2005)
            {
                Enumerate2005Server(reportingServer2005, decodedPath, recursive);
            }
        }