void CreateSchemaNodes(TreeNode connNode)
        {
            Action <TimeSpan> act  = (ts) => LoggingService.Info("Connection " + connNode.Name + ": Schema population completed in " + ts.TotalMilliseconds + "ms");
            FdoConnection     conn = _connMgr.GetConnection(connNode.Name);

            if (conn != null)
            {
                using (FdoFeatureService service = conn.CreateFeatureService())
                {
                    if (service.SupportsPartialSchemaDiscovery())
                    {
                        using (var measure = new TimeMeasurement(act))
                        {
                            List <string> schemaNames = service.GetSchemaNames();

                            //Pre-sort
                            SortedList <string, string> sorted = new SortedList <string, string>();
                            foreach (string name in schemaNames)
                            {
                                sorted.Add(name, name);
                            }

                            foreach (string name in schemaNames)
                            {
                                TreeNode schemaNode = CreateSchemaNode(name, true);
                                connNode.Nodes.Add(schemaNode);
                                schemaNode.Nodes.Add(ResourceService.GetString("TEXT_LOADING"));
                            }
                        }
                    }
                    else
                    {
                        using (var measure = new TimeMeasurement(act))
                        {
                            FeatureSchemaCollection schemas = service.DescribeSchema();

                            //Pre-sort
                            SortedList <string, FeatureSchema> sorted = new SortedList <string, FeatureSchema>();
                            foreach (FeatureSchema schema in schemas)
                            {
                                sorted.Add(schema.Name, schema);
                            }

                            foreach (FeatureSchema schema in schemas)
                            {
                                TreeNode schemaNode = CreateSchemaNode(schema.Name, false);
                                GetClassNodesFull(schema, schemaNode);
                                connNode.Nodes.Add(schemaNode);
                                schemaNode.Expand();
                            }
                        }
                    }
                }
            }
        }
        void OnAfterNodeExpansion(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.Expand)
            {
                TreeNode node = e.Node;
                TreeNode root = _explorer.GetRootNode(RootNodeName);
                //Is a FDO data source node
                if (IsChild(root.Nodes, node, NODE_LEVEL_CLASS))
                {
                    //Find the connection node
                    TreeNode connNode = node;
                    while (connNode.Level > 1)
                    {
                        connNode = connNode.Parent;
                    }
                    string connName = connNode.Name;

                    using (new TempCursor(Cursors.WaitCursor))
                    {
                        switch (node.Level)
                        {
                        case NODE_LEVEL_CONNECTION:     //Connection node
                        {
                            if (!(bool)node.Tag)        //Not loaded, load it now
                            {
                                //Clear out dummy node
                                node.Nodes.Clear();
                                CreateSchemaNodes(node);
                                node.Tag = true;         //Schema is loaded
                            }
                        }
                        break;

                        case NODE_LEVEL_SCHEMA:     //Schema Node
                        {
                            bool isPartial = Convert.ToBoolean(node.Tag);
                            if (isPartial)
                            {
                                Debug.Assert(node.Nodes.Count == 1);         //Has a dummy node
                                string        schemaName = node.Name;
                                FdoConnection conn       = _connMgr.GetConnection(connName);

                                using (FdoFeatureService svc = conn.CreateFeatureService())
                                {
                                    Debug.Assert(svc.SupportsPartialSchemaDiscovery());
                                    List <string> classNames = svc.GetClassNames(schemaName);
                                    GetClassNodesPartial(classNames, node);
                                    node.Tag = false;         //This node is no longer partial
                                    node.Expand();
                                }
                            }
                        }
                        break;

                        case NODE_LEVEL_CLASS:     //Class Node
                        {
                            bool isPartial = Convert.ToBoolean(node.Tag);
                            if (isPartial)
                            {
                                Debug.Assert(node.Nodes.Count == 1);         //Has a dummy node
                                string        schemaName = node.Parent.Name;
                                FdoConnection conn       = _connMgr.GetConnection(connName);
                                using (FdoFeatureService svc = conn.CreateFeatureService())
                                {
                                    Debug.Assert(svc.SupportsPartialSchemaDiscovery());
                                    ClassDefinition cd = svc.GetClassByName(schemaName, node.Name);
                                    if (cd != null)
                                    {
                                        UpdateClassNode(node, cd);
                                    }
                                }
                            }
                        }
                        break;
                        }
                    }
                }
            }
        }
        public override int Execute()
        {
            CommandStatus retCode;

            FdoConnection srcConn  = new FdoConnection(_srcProvider, _srcConnStr);
            FdoConnection destConn = null;

            //Directory given, assume SHP
            if (Directory.Exists(_destPath))
            {
                destConn = new FdoConnection("OSGeo.SHP", "DefaultFileLocation=" + _destPath);
            }
            else
            {
                if (ExpressUtility.CreateFlatFileDataSource(_destPath))
                {
                    destConn = ExpressUtility.CreateFlatFileConnection(_destPath);
                }
                else
                {
                    throw new FdoException("Could not create data source: " + _destPath);
                }
            }

            try
            {
                srcConn.Open();
                destConn.Open();

                string srcName = "SOURCE";
                string dstName = "TARGET";

                FdoBulkCopyOptions options = new FdoBulkCopyOptions();
                options.RegisterConnection(srcName, srcConn);
                options.RegisterConnection(dstName, destConn);
                using (FdoFeatureService srcService = srcConn.CreateFeatureService())
                    using (FdoFeatureService destService = destConn.CreateFeatureService())
                    {
                        //See if spatial context needs to be copied to target
                        if (!string.IsNullOrEmpty(_srcSpatialContext))
                        {
                            SpatialContextInfo srcCtx = srcService.GetSpatialContext(_srcSpatialContext);
                            if (srcCtx != null)
                            {
                                Console.WriteLine("Copying spatial context: " + srcCtx.Name);
                                ExpressUtility.CopyAllSpatialContexts(new SpatialContextInfo[] { srcCtx }, destConn, true);
                            }
                        }
                        else
                        {
                            //Copy all
                            ExpressUtility.CopyAllSpatialContexts(srcConn, destConn, true);
                        }

                        FeatureSchema srcSchema = null;
                        //See if partial class list is needed
                        if (_srcClasses.Count > 0)
                        {
                            WriteLine("Checking if partial schema discovery is supported: " + srcService.SupportsPartialSchemaDiscovery());

                            srcSchema = srcService.PartialDescribeSchema(_srcSchema, _srcClasses);
                        }
                        else //Full copy
                        {
                            WriteLine("No classes specified, reading full source schema");
                            srcSchema = srcService.GetSchemaByName(_srcSchema);
                        }

                        if (srcSchema == null)
                        {
                            WriteError("Could not find source schema: " + _srcSchema);
                            retCode = CommandStatus.E_FAIL_SCHEMA_NOT_FOUND;
                        }
                        else
                        {
                            WriteLine("Checking source schema for incompatibilities");
                            FeatureSchema      targetSchema = null;
                            IncompatibleSchema incSchema;
                            if (destService.CanApplySchema(srcSchema, out incSchema))
                            {
                                int clsCount = srcSchema.Classes.Count;
                                WriteLine("Applying source schema (containing " + clsCount + " classes) to target");
                                destService.ApplySchema(srcSchema, null, true);
                                targetSchema = srcSchema;
                            }
                            else
                            {
                                WriteWarning("Incompatibilities were detected in source schema. Applying a modified version to target");
                                FeatureSchema fixedSchema = destService.AlterSchema(srcSchema, incSchema);
                                int           clsCount    = fixedSchema.Classes.Count;
                                WriteLine("Applying modified source schema (containing " + clsCount + " classes) to target");
                                destService.ApplySchema(fixedSchema, null, true);
                                targetSchema = fixedSchema;
                            }

                            //Now set class copy options
                            foreach (ClassDefinition cd in srcSchema.Classes)
                            {
                                FdoClassCopyOptions copt = new FdoClassCopyOptions(srcName, dstName, srcSchema.Name, cd.Name, targetSchema.Name, cd.Name);
                                copt.FlattenGeometries = _flatten;
                                options.AddClassCopyOption(copt);
                            }

                            if (_flatten)
                            {
                                WriteWarning("The switch -flatten has been defined. Geometries that are copied will have any Z or M coordinates removed");
                            }

                            FdoBulkCopy copy = new FdoBulkCopy(options);
                            copy.ProcessMessage   += new MessageEventHandler(OnMessage);
                            copy.ProcessCompleted += new EventHandler(OnCompleted);
                            Console.WriteLine("Executing bulk copy");
                            copy.Execute();
                            List <Exception> errors = new List <Exception>(copy.GetAllErrors());
                            if (errors.Count > 0)
                            {
                                string file = GenerateLogFileName("bcp-error-");
                                LogErrors(errors, file);
                                base.WriteError("Errors were encountered during bulk copy.");
                                retCode = CommandStatus.E_FAIL_BULK_COPY_WITH_ERRORS;
                            }
                            else
                            {
                                retCode = CommandStatus.E_OK;
                            }
                            retCode = CommandStatus.E_OK;
                        }
                    }
            }
            catch (Exception ex)
            {
                WriteException(ex);
                retCode = CommandStatus.E_FAIL_UNKNOWN;
            }
            finally
            {
                srcConn.Dispose();
                destConn.Dispose();
            }
            return((int)retCode);
        }