/// <summary>
        /// 打开一个SDE数据连接。
        /// </summary>
        /// <param name="propertySet">连接信息。</param>
        /// <returns>工作空间。</returns>
        public static IWorkspace OpenSdeWorkspace(IPropertySet propertySet)
        {
            Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
            IWorkspaceFactory2 workspaceFactory = (IWorkspaceFactory2)Activator.CreateInstance(factoryType);

            return(workspaceFactory.Open(propertySet, 0));
        }
        public static ILocatorWorkspace GetSDELocatorWorkspace(String server, String instance, String database, String authenication, String version)
        {
            // Set up the SDE connection properties
            IPropertySet connectionProperties = new PropertySetClass();

            connectionProperties.SetProperty("SERVER", server);
            //propertySet.SetProperty("DBCLIENT", dbclient);
            connectionProperties.SetProperty("INSTANCE", instance);
            connectionProperties.SetProperty("DATABASE", database);
            connectionProperties.SetProperty("AUTHENTICATION_MODE", authenication);
            connectionProperties.SetProperty("VERSION", version);
            //connectionProperties.SetProperty("USER", username);
            //connectionProperties.SetProperty("PASSWORD", pass);

            // Get the Workspace
            System.Object      obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"));
            IWorkspaceFactory2 workspaceFactory = obj as IWorkspaceFactory2;
            IWorkspace         workspace        = workspaceFactory.Open(connectionProperties, 0);

            obj = Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
            ILocatorManager2          locatorManager           = obj as ILocatorManager2;
            ILocatorWorkspace         locatorWorkspace         = locatorManager.GetLocatorWorkspace(workspace);
            IDatabaseLocatorWorkspace databaseLocatorWorkspace = (IDatabaseLocatorWorkspace)locatorWorkspace;

            return(locatorWorkspace);
        }
        /// <summary>
        /// 添加数据库连接,连接的数据库类型为PostgreSQL,Oracle等
        /// </summary>
        /// <param name="type">数据库类型,如postgresql,Oracle等</param>
        /// <param name="IP">IP地址</param>
        /// <param name="database">数据库</param>
        /// <param name="user">用户名</param>
        /// <param name="pwd">密码</param>
        ///
        public void ConnectPGDB(string type, string IP, string database, string user, string pwd)
        {
            IAoInitialize m_AoInitialize = new AoInitialize();

            //必须添加ArcServer许可
            m_AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcServer);

            IPropertySet pPropertySet = new PropertySetClass();

            pPropertySet.SetProperty("SERVER", IP);
            pPropertySet.SetProperty("INSTANCE", "sde:" + type + ":" + IP);
            pPropertySet.SetProperty("DATABASE", database);
            pPropertySet.SetProperty("USER", user);
            pPropertySet.SetProperty("PASSWORD", pwd);

            pWorkspace = pWorkspaceFactory.Open(pPropertySet, 0);
            MessageBox.Show("数据库连接成功");
        }
        private IFeatureClass GetFeatureClassInsideDataset(string featureClassName)
        {
            IWorkspaceFactory2 factory = null;

            if (_serverContext != null)
            {
                factory = (IWorkspaceFactory2)_serverContext.CreateObject("esriDataSourcesGDB.SDEWorkspaceFactory");
            }
            //else
            //    factory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
            //TODO COMMENTED ABOVE TO ALLOW COMPILE
            IWorkspace        ipWks            = null;
            IFeatureClass     ipFeatClass      = null;
            IFeatureDataset   ipFeatDS         = null;
            IEnumDataset      ipEnumSubDataset = null;
            IEnumDataset      ipEnumDataset    = null;
            IFeatureWorkspace fws = null;

            try
            {
                IPropertySet2 propSet = null;
                if (_serverContext != null)
                {
                    propSet = (IPropertySet2)_serverContext.CreateObject("esriSystem.PropertySet");
                }
                //else
                //    propSet =  new ESRI.ArcGIS.esriSystem.PropertySetClass();
                //TODO COMMENTED ABOVE TO ALLOW COMPILE
                propSet.SetProperty("SERVER", _sdeServerInfo.server);
                propSet.SetProperty("INSTANCE", _sdeServerInfo.instance);
                propSet.SetProperty("DATABASE", _sdeServerInfo.database);
                propSet.SetProperty("USER", _sdeServerInfo.user);
                propSet.SetProperty("PASSWORD", _sdeServerInfo.password);
                propSet.SetProperty("VERSION", _sdeServerInfo.version);

                ipWks = factory.Open(propSet, 0);
                fws   = (IFeatureWorkspace)ipWks;
                //try and open it
                if (ipWks != null)
                {
                    // Now get all the feature classes included in the datasets
                    ipEnumDataset = ipWks.get_Datasets(esriDatasetType.esriDTFeatureDataset);
                    ipEnumDataset.Reset();
                    ipFeatDS = ipEnumDataset.Next() as IFeatureDataset;
                    while (ipFeatDS != null)
                    {
                        ipEnumSubDataset = ipFeatDS.Subsets; //.get_Datasets(esriDatasetType.esriDTFeatureClass);
                        ipEnumSubDataset.Reset();
                        ipFeatClass = ipEnumSubDataset.Next() as IFeatureClass;
                        while (ipFeatClass != null)
                        {
                            if (ipFeatClass.AliasName.ToUpper().Contains(featureClassName.ToUpper()))
                            {
                                return(ipFeatClass);
                            }
                            ipFeatClass = ipEnumSubDataset.Next() as IFeatureClass;
                        }
                        ipFeatDS         = ipEnumDataset.Next() as IFeatureDataset;
                        ipEnumSubDataset = null;
                    }

                    ipEnumDataset = null;
                }
            }
            catch (System.Runtime.InteropServices.COMException edd)
            {
                ipFeatClass = null;
                throw edd;
            }
            finally
            {
                Util.ReleaseCOMObject(fws);
                Util.ReleaseCOMObject(ipEnumSubDataset);
                Util.ReleaseCOMObject(ipEnumDataset);
                Util.ReleaseCOMObject(factory);
            }

            return(null);
        }
        //search outside datasets
        protected IFeatureClass GetFeatureClass(string featureClassName)
        {
            IWorkspaceFactory2 factory = null;

            if (_serverContext != null)
            {
                factory = (IWorkspaceFactory2)_serverContext.CreateObject("esriDataSourcesGDB.SDEWorkspaceFactory");
            }

            IWorkspace        ipWks            = null;
            IFeatureClass     ipFeatClass      = null;
            IFeatureClass     ipFeatDS         = null;
            IFeatureClass     ipEnumSubDataset = null;
            IEnumDataset      ipEnumDataset    = null;
            IFeatureWorkspace fws = null;

            try
            {
                IPropertySet2 propSet = null;
                if (_serverContext != null)
                {
                    propSet = (IPropertySet2)_serverContext.CreateObject("esriSystem.PropertySet");
                }

                propSet.SetProperty("SERVER", _sdeServerInfo.server);
                propSet.SetProperty("INSTANCE", _sdeServerInfo.instance);
                propSet.SetProperty("DATABASE", _sdeServerInfo.database);
                propSet.SetProperty("USER", _sdeServerInfo.user);
                propSet.SetProperty("PASSWORD", _sdeServerInfo.password);
                propSet.SetProperty("VERSION", _sdeServerInfo.version);

                ipWks = factory.Open(propSet, 0);
                fws   = (IFeatureWorkspace)ipWks;

                //try and open it
                if (ipWks != null)
                {
                    ipEnumDataset = ipWks.get_Datasets(esriDatasetType.esriDTFeatureClass);
                    ipEnumDataset.Reset();
                    ipFeatDS = ipEnumDataset.Next() as IFeatureClass;
                    while (ipFeatDS != null)
                    {
                        ipEnumSubDataset = ipFeatDS;
                        ipFeatClass      = ipEnumSubDataset;
                        while (ipFeatClass != null)
                        {
                            if (ipFeatClass.AliasName.ToUpper().Contains(featureClassName.ToUpper()))
                            {
                                return(ipFeatClass);
                            }
                            ipFeatClass = ipEnumDataset.Next() as IFeatureClass;
                        }
                        ipFeatDS         = ipEnumDataset.Next() as IFeatureClass;
                        ipEnumSubDataset = null;
                    }

                    ipEnumDataset = null;
                }
            }
            catch (System.Runtime.InteropServices.COMException edd)
            {
                //exception is coming from Arcobjects...
                ipFeatClass = null;
                throw edd;
            }
            finally
            {
                Util.ReleaseCOMObject(fws);
                Util.ReleaseCOMObject(ipEnumSubDataset);
                Util.ReleaseCOMObject(ipEnumDataset);
                Util.ReleaseCOMObject(factory);
            }
            return(GetFeatureClassInsideDataset(featureClassName));
        }