ArcSDE转本地MDB数据库

一个转换类,将ArcSDE上特定要素数据集转到本地的MDB中


using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.ConversionTools;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.esriSystem;
namespace EngineWindowsApplication1
{
    class C_SDEToLocalMDB
    {
        /// <summary>
        /// 基于Oracle的SDE要素数据集转本地MDB要素数据集
        /// </summary>
        /// <param name="IN_Parameters">参数数组[0]:实例 [1]:用户名 [2]:密码</param>
        /// <param name="IN_MDBFile">生成的本地MDB的路径</param>
        /// <param name="IN_MDBName">生成的MDB的名字</param>
        /// <param name="IN_FeatureClassName">SDE上目标要素类的名称</param>
        public C_SDEToLocalMDB(string[] IN_Parameters, string IN_MDBFile, string IN_MDBName, string IN_FeatureClassName)
        {
            IWorkspace2 Temp_SDEWorkspace;//SDE上工作空间
            #region 连接SDE
            try
            {
                //通过IPropertySet设置通过SDE连接数据库的各种参数
                IPropertySet Temp_PropertySet = new PropertySetClass();
                Temp_PropertySet.SetProperty("INSTANCE", IN_Parameters[0]);// 数据库实例
                Temp_PropertySet.SetProperty("USER", IN_Parameters[1]);//用户名
                Temp_PropertySet.SetProperty("PASSWORD", IN_Parameters[2]);//密码
                IWorkspaceFactory Temp_WorkFactory = new SdeWorkspaceFactory();
                Temp_SDEWorkspace = (IWorkspace2)Temp_WorkFactory.Open(Temp_PropertySet, 0);
            }
            catch
            {
                return;
            }
            #endregion
            //打开SDE上的目标要素数据集
            IFeatureDataset Temp_SdeFeatureDataset = (Temp_SDEWorkspace as IFeatureWorkspace).OpenFeatureDataset(IN_FeatureClassName);
            //构建本地MDB
            IWorkspaceFactory Temp_LocalAccessWorkFactory = new AccessWorkspaceFactory();
            string Temp_MDBFile = IN_MDBFile + IN_MDBName + ".mdb";
            string Temp_LDBFile = IN_MDBFile + IN_MDBName + ".ldb";
            if (File.Exists(Temp_MDBFile))//清理MDB和LDB文件
            {
                File.Delete(Temp_MDBFile);
                if (File.Exists(Temp_LDBFile))
                    File.Delete(Temp_LDBFile);
            }
            Temp_LocalAccessWorkFactory.Create(IN_MDBFile, IN_MDBName, null, 0);//创建一个MDB
            //本地MDB的工作空间
            IFeatureWorkspace Temp_MDBWorkspace = Temp_LocalAccessWorkFactory.OpenFromFile(Temp_MDBFile, 0) as IFeatureWorkspace;
            //在本地创建同名要素数据集
            IFeatureDataset Temp_LocalDataset = Temp_MDBWorkspace.CreateFeatureDataset(IN_FeatureClassName, (Temp_SdeFeatureDataset as IGeoDataset).SpatialReference);
            //批量导入要素类
            for (int i = 0; i < (Temp_SdeFeatureDataset as IFeatureClassContainer).ClassCount; i++)
            {
                PRV_ConvertFeatureClass(Temp_SdeFeatureDataset.Workspace, Temp_LocalDataset, (Temp_SdeFeatureDataset as IFeatureClassContainer).get_Class(i).AliasName, (Temp_SdeFeatureDataset as IFeatureClassContainer).get_Class(i).AliasName);
            }
        }


        /// <summary>
        /// 将一个要素类从一个工作空间转移到另外一个工作空间
        /// 注意目标工作空间不能有改要素类,必须先清除  
        /// </summary>
        /// <param name="IN_SourceWorkspace">源工作空间</param>
        /// <param name="IN_TargetWorkspace">目标要素数据集</param>
        /// <param name="IN_SourceFeatureClassName">源要素类名</param>
        /// <param name="IN_TargetFeatureClassName">目标要素类名</param>
        private void PRV_ConvertFeatureClass(IWorkspace IN_SourceWorkspace, IFeatureDataset IN_TargetWorkspace, string IN_SourceFeatureClassName, string IN_TargetFeatureClassName)
        {
            #region 环境配置
            //创建源工作空间名  
            IDataset Temp_SourceWorkspaceDataset = (IDataset)IN_SourceWorkspace;
            IWorkspaceName Temp_SourceWorkspaceName = (IWorkspaceName)Temp_SourceWorkspaceDataset.FullName;
            //创建源要素数据集名  
            IFeatureClassName Temp_SourceFeatureClassName = new FeatureClassNameClass();
            IDatasetName Temp_SourceDatasetName = (IDatasetName)Temp_SourceFeatureClassName;
            Temp_SourceDatasetName.WorkspaceName = Temp_SourceWorkspaceName;
            Temp_SourceDatasetName.Name = IN_SourceFeatureClassName;
            //创建目标工作空间名    
            IDataset Temp_TargetWorkspaceDataset = (IDataset)IN_TargetWorkspace.Workspace;
            IWorkspaceName Temp_TargetWorkspaceName = (IWorkspaceName)(Temp_TargetWorkspaceDataset.FullName);
            //创建目标要素类名  
            IFeatureClassName Temp_TargetFeatureClassName = new FeatureClassNameClass();
            IDatasetName Temp_TargetDatasetName = (IDatasetName)Temp_TargetFeatureClassName;
            Temp_TargetDatasetName.WorkspaceName = Temp_TargetWorkspaceName;
            Temp_TargetDatasetName.Name = IN_TargetFeatureClassName;
            //创建目标要素数据集名  
            IFeatureDatasetName Temp_TargetFeatureDatasetName = new FeatureDatasetNameClass();
            IDatasetName Temp_TargetDatasetName2 = (IDatasetName)Temp_TargetFeatureDatasetName;
            Temp_TargetDatasetName2.WorkspaceName = Temp_TargetWorkspaceName;
            Temp_TargetDatasetName2.Name = IN_TargetWorkspace.Name;
            #endregion
            //打开源要素类获取字段定义 
            ESRI.ArcGIS.esriSystem.IName Temp_SourceName = (ESRI.ArcGIS.esriSystem.IName)Temp_SourceFeatureClassName;
            IFeatureClass Temp_SourceFeatureClass = (IFeatureClass)Temp_SourceName.Open();
            //验证字段 
            IFieldChecker Temp_FieldChecker = new FieldCheckerClass();
            IFields Temp_TargetFeatureClassFields;
            IFields Temp_SourceFeatureClassFields = Temp_SourceFeatureClass.Fields;
            IEnumFieldError enumFieldError;
            Temp_FieldChecker.InputWorkspace = IN_SourceWorkspace;
            Temp_FieldChecker.ValidateWorkspace = IN_TargetWorkspace.Workspace;
            Temp_FieldChecker.Validate(Temp_SourceFeatureClassFields, out enumFieldError, out Temp_TargetFeatureClassFields);
            //批量导入 
            IField Temp_GeometryField;
            for (int i = 0; i < Temp_TargetFeatureClassFields.FieldCount; i++)
            {
                if (Temp_TargetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
                {
                    Temp_GeometryField = Temp_TargetFeatureClassFields.get_Field(i);
                    //获取空间定义           
                    IGeometryDef Temp_GeometryDef = Temp_GeometryField.GeometryDef;
                    IGeometryDefEdit Temp_TargetFCGeoDefEdit = (IGeometryDefEdit)Temp_GeometryDef;
                    Temp_TargetFCGeoDefEdit.GridCount_2 = 1;
                    Temp_TargetFCGeoDefEdit.set_GridSize(0, 0);
                    Temp_TargetFCGeoDefEdit.SpatialReference_2 = Temp_GeometryField.GeometryDef.SpatialReference;
                    //定义筛选条件 
                    IQueryFilter Temp_QueryFilter = new QueryFilterClass();
                    Temp_QueryFilter.WhereClause = "";
                    //导入要素类至要素数据集  
                    IFeatureDataConverter Temp_FeatureDataConverter = new FeatureDataConverterClass();
                    IEnumInvalidObject enumErrors = Temp_FeatureDataConverter.ConvertFeatureClass(Temp_SourceFeatureClassName, Temp_QueryFilter, Temp_TargetFeatureDatasetName, Temp_TargetFeatureClassName, Temp_GeometryDef, Temp_TargetFeatureClassFields, "", 1000, 0);
                    break;
                }
            }

        }

    }
}



你可能感兴趣的:(arcsde,arcgis,ArcEngine,ArcgisEngine)