Ejemplo n.º 1
0
        protected void SaveCachedObject(String basePath, ObjectCache.CachedObject cachedObj)
        {
            Server server = new Server(_server.Name);
            InitServer(server);

            Database database = server.Databases[_database.Name];

            String folder = Path.Combine(basePath, cachedObj.Label);
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);

            SqlSmoObject obj = cachedObj.Activate(database);
            SmoWrapper wrapper = new SmoWrapper(obj);
            if (wrapper.IsSystemObject)
                return;

            using (ScriptWriter scriptWriter = new ScriptWriter(
                    new StreamWriter(
                        Path.Combine(folder, String.Format("{0}.sql",
                        StringUtils.NormalizeFilename(
                            wrapper.QualifiedName)))), _options))
            {
                scriptWriter.WriteDefinition(wrapper);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Exports structure to a folder (not zipped.)
        /// If the folder already exists, existing files/folders won't be deleted.
        /// </summary>
        /// <param name="basePath">Folder to save to</param>
        public void SaveFolder(String folder)
        {
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);

            // Multithreaded
            ObjectCache cache = new ObjectCache(_server, _database);
            int workerCount = cache.Count;
            ManualResetEvent finishedEvent = new ManualResetEvent(false);

            int maxWorkers, maxIOCP;
            ThreadPool.GetMaxThreads(out maxWorkers, out maxIOCP);
            Console.WriteLine("Queueing {0} workers using {1} threads...", workerCount, maxWorkers);
            if (workerCount == 0)
            {
                finishedEvent.Set();
            }
            else
            {
                for (int i = 0; i < workerCount; ++i)
                {
                    ThreadPool.QueueUserWorkItem(delegate(Object state)
                    {
                        SaveCachedObject(folder, cache.GetNext());

                        Interlocked.Decrement(ref workerCount);
                        if (workerCount == 0)
                            finishedEvent.Set();
                    });
                }
            }

            InitServer(_server);
            SaveObjects(folder, "Schemas", _database.Schemas,
                delegate(SmoWrapper obj)
                {
                    return !StringUtils.IsInArray(obj.Name, _ignoredSchemas, true);
                });
            SaveObjects(folder, "XML Schemas", _database.XmlSchemaCollections);
            SaveObjects(folder, "Users", _database.Users,
                delegate(SmoWrapper obj)
                {
                    return !StringUtils.IsInArray(obj.Name, _ignoredUsers, true);
                });
            SaveObjects(folder, "Roles", _database.Roles,
                delegate(SmoWrapper obj)
                {
                    return !StringUtils.IsInArray(obj.Name, _ignoredRoles, true);
                });
            SaveObjects(folder, "Assemblies", _database.Assemblies);
            SaveObjects(folder, "Types", _database.UserDefinedTypes);
            SaveObjects(folder, "Data Types", _database.UserDefinedDataTypes);
            SaveObjects(folder, "Aggregates", _database.UserDefinedAggregates);
            SaveObjects(folder, "Synonyms", _database.Synonyms);
            SaveObjects(folder, "Partition Functions", _database.PartitionFunctions);
            SaveObjects(folder, "Partition Schemes", _database.PartitionSchemes);
            SaveObjects(folder, "Rules", _database.Rules);
            SaveObjects(folder, "Triggers", _database.Triggers);

            finishedEvent.WaitOne(Timeout.Infinite, true);
        }