public DatabaseInventory(ManagementConnectionProvider database, string schemaName)
     : base()
 {
     if (database == null) {
         throw new ArgumentNullException("database");
     }
     targetEngine = database.Engine;
     this.schemaName = schemaName;
     XsltArgumentList arguments = CreateArguments(database);
     using (SqlCommand command = database.GetConnection().CreateCommand()) {
         command.Transaction = database.GetTransaction();
         command.CommandType = CommandType.Text;
         command.Parameters.AddWithValue("@sSchema", schemaName);
         using (StringWriter writer = new StringWriter()) {
             userObjectList.Transform(new XDocument().CreateReader(), arguments, writer);
             command.CommandText = Regex.Replace(writer.ToString(), @"\s+", " ");
             // ReSharper disable AccessToDisposedClosure
             log.Trace(l => l(writer.ToString()));
             // ReSharper restore AccessToDisposedClosure
         }
         using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleResult)) {
             int definitionColumn = reader.GetOrdinal("xDefinition");
             int nameColumn = reader.GetOrdinal("sName");
             StringBuilder builder = new StringBuilder(65520);
             while (reader.Read()) {
                 builder.Length = 0;
                 using (StringWriter writer = new StringWriter(builder)) {
                     SqlXml xml = reader.GetSqlXml(definitionColumn);
                     scripter.Transform(xml.CreateReader(), arguments, writer);
                     // ReSharper disable AccessToDisposedClosure
                     log.Trace(l => l("Object name: {0}\n  XML: {1}\n  SQL: {2}", reader.GetString(nameColumn), xml.Value, writer));
                     // ReSharper restore AccessToDisposedClosure
                 }
                 try {
                     try {
                         using (StringReader scriptReader = new StringReader(builder.ToString())) {
                             CreateStatement objectStatement = ProcessSingleScript(scriptReader, statement => {
                                 log.Error(l => l("Cannot process statement error: {0}", statement));
                                 throw CreateException("Cannot process statement:", statement, TargetEngine);
                             }).SingleOrDefault(statement => objectsToRename.Any(t => t.IsAssignableFrom(statement.GetType())));
                             if (objectStatement != null) {
                                 objectStatement.ObjectName = reader.GetString(nameColumn);
                             }
                         }
                     } catch (ParseException ex) {
                         ex.FileName = reader.GetString(nameColumn);
                         throw;
                     }
                 } catch {
                     Trace.WriteLine(builder.ToString());
                     throw;
                 }
             }
         }
     }
 }
 private XsltArgumentList CreateArguments(ManagementConnectionProvider database)
 {
     XsltArgumentList arguments = new XsltArgumentList();
     arguments.AddExtensionObject("urn:utils", new XsltUtils());
     arguments.AddParam("engine", "", targetEngine.ToString());
     arguments.AddParam("azure", "", targetEngine == DatabaseEngine.SqlAzure);
     arguments.AddParam("version", "", database.EngineVersion.Major);
     return arguments;
 }