Ejemplo n.º 1
0
        /// <summary>
        /// Saves all objects in database as *.sql scripts in subdirectories
        /// organized by object type.
        /// </summary>
        /// <param name="basePath">Base directory to save scripts to.</param>
        /// <param name="label">Label of subfolder.</param>
        /// <param name="collection">Collection of SMOs to save.</param>
        /// <param name="extendedSave">Optional custom save delegate (if not null, callee is completely responsible for saving.)</param>
        protected void SaveObjects(String basePath, String label, 
            SmoCollectionBase collection, SaveAllowedDelegate saveAllowed)
        {
            String folder = Path.Combine(basePath, label);
            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);

            foreach (SqlSmoObject obj in collection)
            {
                SmoWrapper wrapper = new SmoWrapper(obj);
                if (wrapper.IsSystemObject)
                    continue;

                if (saveAllowed == null || (saveAllowed != null && saveAllowed(wrapper)))
                {
                    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
        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);
            }
        }