persongeodatabase(mdb格式)导入到SDE的数据库中
//将上传的mdb文件保存至sde数据库中
protected void btnSaveToSDE_Click(object sender, EventArgs e)
{
//获取服务器上下文
IServerContext serverContext = GetServerContext();
//打开mdb文件所在的工作空间
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory wf = serverContext.CreateObject("esriDataSourcesGDB.AccessWorkspaceFactory") as IWorkspaceFactory;
string fileName = @"F:\MyGeodatabase.mdb";
IFeatureWorkspace pFeatureWorkspaceMDB = wf.OpenFromFile(fileName, 0) as IFeatureWorkspace;
IWorkspace pWorkspaceMDB = pFeatureWorkspaceMDB as IWorkspace;
//打开sde空间数据库的工作空间
IFeatureWorkspace pFeatureWorkspaceSDE = OpenWorkspace(serverContext);
IWorkspace pWorkspaceSDE = pFeatureWorkspaceSDE as IWorkspace;
#region //获得mdb所有的要素数据集并进行转换复制
IEnumDataset pEnumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureDataset);
IDataset pDataset = pEnumDataset.Next();
//遍历mdb中每一个数据集
string nameOfSourceFeatureDataset = null;//源数据集名称
string nameOfTargetFeatureDataset = null;//目标数据集名称
while (pDataset != null)
{
nameOfSourceFeatureDataset = pDataset.Name;
nameOfTargetFeatureDataset = nameOfSourceFeatureDataset + "_SDE";
ConvertMDBFeatureDatasetToSDE(serverContext, pWorkspaceMDB, pWorkspaceSDE, nameOfSourceFeatureDataset, nameOfTargetFeatureDataset);
pDataset = pEnumDataset.Next();
}
#endregion
#region //获得mdb中所有的独立要素类并进行转换复制
IEnumDataset enumDataset = pWorkspaceMDB.get_Datasets(esriDatasetType.esriDTFeatureClass) as IEnumDataset;
IDataset dataset = enumDataset.Next();
//遍历mdb的每一个独立要素类
string nameOfSourceFeatureClass = null;//源要素类名称
string nameOfTargetFeatureClass= null;//目标要素类名称
while (dataset != null)
{
if (dataset.Type == esriDatasetType.esriDTFeatureClass)
{
IFeatureClass pFeatureClass = dataset as IFeatureClass;
nameOfSourceFeatureClass = pFeatureClass.AliasName;
nameOfTargetFeatureClass = nameOfSourceFeatureClass + "_SDE";
ConvertMDBFeatureClassToSDE(serverContext, pWorkspaceMDB, pWorkspaceSDE, nameOfSourceFeatureClass, nameOfTargetFeatureClass);
dataset = enumDataset.Next();
}
else
{
return;
}
}
#endregion
//释放服务器上下文
serverContext.ReleaseContext();
}
//获得服务器上下文
public IServerContext GetServerContext()
{
#region 通过IGISServerConnection来创建服务器上下文
IGISServerConnection gisServerConnection = new GISServerConnection();
gisServerConnection.Connect("服务器名");
IServerObjectManager4 serverObjectManager = (IServerObjectManager4)gisServerConnection.ServerObjectManager;
IServerContext serverContext = serverObjectManager.CreateServerContext("", "");
#endregion
return serverContext;
}
//打开sde数据库
public IFeatureWorkspace OpenWorkspace(IServerContext serverContext)
{
//sde数据库连接属性设置
IPropertySet pProperty = serverContext.CreateObject("esriSystem.PropertySet") as IPropertySet;
pProperty.SetProperty("Server", "fms");//服务器名称或者IP
pProperty.SetProperty("Instance", "esri_sde");//
pProperty.SetProperty("Database", "sde数据库名");//sde数据库名称
pProperty.SetProperty("user", "用户名");//用户名称
pProperty.SetProperty("password", "密码");//用户密码
pProperty.SetProperty("version", "sde.DEFAULT");//连接版本
//创建IWorkspaceFactory对象
ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2 wf = serverContext.CreateObject("esriDataSourcesGDB.SdeWorkspaceFactory") as IWorkspaceFactory2;
//打开IFeatureWorkspace对象
IFeatureWorkspace fws = wf.Open(pProperty, 0) as IFeatureWorkspace;
return fws;
}
//将mdb中的要素数据集转换到sde数据库中
public void ConvertMDBFeatureDatasetToSDE(IServerContext serverContext,IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset)
{
if (sourceWorkspace == null || targetWorkspace == null)
{
return;
}
//创建源工作空间名
IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
//创建源数据集名
IFeatureDatasetName sourceFeatureDatasetName = serverContext.CreateObject("esriGeoDatabase.FeatureDatasetName") as IFeatureDatasetName;
IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName;
sourceDatasetName.WorkspaceName = sourceWorkspaceName;
sourceDatasetName.Name = nameOfSourceFeatureDataset;
//创建目标工作空间名
IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
//创建目标数据集名
IFeatureDatasetName targetFeatureDatasetName = serverContext.CreateObject("esriGeoDatabase.FeatureDatasetName") as IFeatureDatasetName;
IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName;
targetDatasetName.WorkspaceName = targetWorkspaceName;
targetDatasetName.Name = nameOfTargetFeatureDataset;
//转换(复制)源数据集到目标数据集
IFeatureDataConverter featureDataConverter = serverContext.CreateObject("esriGeoDatabase.FeatureDataConverter") as IFeatureDataConverter;
featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName, targetFeatureDatasetName, null, "", 1000, 0);
}
//将mdb中要素类转换复制到sde数据库中
public void ConvertMDBFeatureClassToSDE(IServerContext serverContext,IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
{
// 创建源工作空间名称
IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
//创建源数据集名称
IFeatureClassName sourceFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName;
IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
sourceDatasetName.WorkspaceName = sourceWorkspaceName;
sourceDatasetName.Name = nameOfSourceFeatureClass;
//创建目标工作空间名称
IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
//创建目标数据集名称
IFeatureClassName targetFeatureClassName = serverContext.CreateObject("esriGeoDatabase.FeatureClassName") as IFeatureClassName;
IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
targetDatasetName.WorkspaceName = targetWorkspaceName;
targetDatasetName.Name = nameOfTargetFeatureClass;
//打开输入的要素类以得到字段定义
ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();//打开源要素类
//验证字段名称,因为你正在不同类型的工作空间之间进行数据转换
IFieldChecker fieldChecker = serverContext.CreateObject("esriGeoDatabase.FieldChecker") as IFieldChecker;
IFields sourceFeatureClassFields = sourceFeatureClass.Fields;
IFields targetFeatureClassFields;
IEnumFieldError enumFieldError;
//最重要的设置输入和验证工作空间
fieldChecker.InputWorkspace = sourceWorkspace;
fieldChecker.ValidateWorkspace = targetWorkspace;
fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);
//遍历所有输出字段找到几何字段
IField geometryField;
for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
{
if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
{
geometryField = targetFeatureClassFields.get_Field(i);
//得到几何字段的几何定义
IGeometryDef geometryDef = geometryField.GeometryDef;
//赋予几何定义一个空间索引格网数目和格网大小值
IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
targetFCGeoDefEdit.GridCount_2 = 1;
targetFCGeoDefEdit.set_GridSize(0, 0);
//允许ArcGIS为数据加载确定一个有效的格网大小
targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
//转换要素类中所有的要素
IQueryFilter queryFilter = serverContext.CreateObject("esriGeoDatabase.QueryFilter") as IQueryFilter; ;
queryFilter.WhereClause = "";
//加载要素类
IFeatureDataConverter fctofc = serverContext.CreateObject("esriGeoDatabase.FeatureDataConverter") as IFeatureDataConverter;
IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
break;
}
}
}