1、sde连接
private bool connectSde(string sdeInfo) { bool flag = false; try { IWorkspaceFactory worksapceFactory = new SdeWorkspaceFactoryClass(); IPropertySet propertySet = new PropertySet(); if (sdeInfo.Contains(";")) { propertySet.SetProperty("Server", sdeInfo.Split(';')[0]); propertySet.SetProperty("Instance", sdeInfo.Split(';')[1]); propertySet.SetProperty("User", sdeInfo.Split(';')[2]); propertySet.SetProperty("Password", sdeInfo.Split(';')[3]); propertySet.SetProperty("Version", "SDE.DEFAULT"); } workspace = worksapceFactory.Open(propertySet, 0); if (workspace != null) { flag = true; } else { flag = false; } } catch (Exception ex) { flag = false; } return flag; }
2、打开sde中的数据集
1 private IFeatureDataset getFeatureDataSet(IWorkspace g_workspace, string layerName) 2 { 3 IFeatureDataset m_FeatureDataset = null; 4 IFeatureWorkspace pWS1 = g_workspace as IFeatureWorkspace; 5 6 if (pWS1 == null) 7 { 8 return m_FeatureDataset; 9 } 10 11 if (pWS1 != null) 12 { 13 try 14 { 15 m_FeatureDataset = pWS1.OpenFeatureDataset(layerName); //打开要素类 16 } 17 catch (Exception e) 18 { 19 return null; 20 } 21 } 22 return m_FeatureDataset; 23 }
3、打开sde中的图层
private IFeatureClass getFeatureClass(IWorkspace g_workspace, string layerName) { IFeatureClass m_FeatureClass = null; IFeatureWorkspace pWS1 = g_workspace as IFeatureWorkspace; if (pWS1 == null) { return m_FeatureClass; } if (pWS1 != null) { try { m_FeatureClass = pWS1.OpenFeatureClass(layerName); //打开要素类 } catch (Exception e) { return null; } } return m_FeatureClass; }
4、sde上删除图层
public bool DeleteFromSde(string layerName,IList<string> layerNameList) { bool flag = true; try { IFeatureDataset pFeatureDataset = getFeatureDataSet(workspace, layerName); if (pFeatureDataset != null) { IFeatureClassContainer m_FeatureClassContainer = (IFeatureClassContainer)pFeatureDataset; IEnumFeatureClass m_EnumFC = m_FeatureClassContainer.Classes; IFeatureClass m_FeatureClass = m_EnumFC.Next(); while (m_FeatureClass != null ) { if (layerNameList.Contains(m_FeatureClass.AliasName.ToUpper())) { IQueryFilter QueryFilter = new QueryFilterClass(); if (m_FeatureClass.FeatureCount(QueryFilter) > 0) { //判断是否支持 //(1)判断是否支持SQL语句 IWorkspaceProperties pWspProperties = workspace as IWorkspaceProperties; IWorkspaceProperty pWspProperty = pWspProperties.get_Property(esriWorkspacePropertyGroupType.esriWorkspacePropertyGroup, (int)esriWorkspacePropertyType.esriWorkspacePropCanExecuteSQL); if (!pWspProperty.IsSupported) { flag = false; } //删除数据 IDataset pDataset = pFeatureDataset as IDataset; pDataset.Workspace.ExecuteSQL("delete from " + m_FeatureClass.AliasName + " where " + m_FeatureClass.OIDFieldName+" > 0" ); } } m_FeatureClass = m_EnumFC.Next(); } } } catch (Exception ex) { flag = false; } return flag; }
5、向sde数据集中追加对象集
private bool AddToSde(IFeatureClass insertFeatureClass, string sdefeaName,out string message) { Geoprocessor gp = new Geoprocessor(); bool flag = false; message = ""; try { IDataset insertDs = insertFeatureClass as IDataset; IFeatureClass pFeatureClass = getFeatureClass(workspace, sdefeaName); IDataset sdeDs = pFeatureClass as IDataset; ESRI.ArcGIS.DataManagementTools.Append appendTool = new Append(); appendTool.inputs = insertDs; appendTool.target = sdeDs; gp.Execute(appendTool, null); flag = true; } catch (Exception ex) { message = ex.ToString(); flag = false; } finally { for (int count = 0; count < gp.MessageCount; count++) { message += gp.GetMessage(count).ToString(); } } return flag; }
6、遍历sde数据集并将之加到treeview中
private void LoadSdeData() { Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); //通过IPropertySet设置通过SDE连接数据库的各种参数 IPropertySet propertySet = new PropertySetClass(); propertySet.SetProperty("SERVER", sdeInfo.ServerIp); propertySet.SetProperty("INSTANCE", sdeInfo.Port); propertySet.SetProperty("USER", sdeInfo.UserName); propertySet.SetProperty("PASSWORD", sdeInfo.UserPss); propertySet.SetProperty("VERSION", "sde.DEFAULT"); //通过以上设置的参数将数据库的数据通过SDE读入工作空间 IWorkspace workspace = workspaceFactory.Open(propertySet, 0); IEnumDataset enumDataSet = workspace.get_Datasets(esriDatasetType.esriDTAny); enumDataSet.Reset(); //再把Enum数据集中的数据一个个读到DataSet中 IDataset dataSet; dataSet = enumDataSet.Next(); //判断数据集是否有数据 while(dataSet!=null) { //判断数据集中的数据是什么类型 if(dataSet is IFeatureDataset) { TreeNode dsNode = new TreeNode(); //如果是FeatureDataSet做以下处理 IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; IFeatureDataset featureDataset = featureWorkspace.OpenFeatureDataset(dataSet.Name); IFeatureClassContainer m_FeatureClassContainer = (IFeatureClassContainer)featureDataset; IEnumFeatureClass m_EnumFC = m_FeatureClassContainer.Classes; IFeatureClass m_FeatureClass = m_EnumFC.Next(); if (m_FeatureClass != null) { dsNode.Text = dataSet.Name; dsNode.Tag = featureDataset; while (m_FeatureClass != null) { IFeatureLayer m_FeatureLayer = new FeatureLayerClass(); m_FeatureLayer.FeatureClass = m_FeatureClass; m_FeatureLayer.Name = m_FeatureClass.AliasName; TreeNode layerNode = new TreeNode(m_FeatureLayer.FeatureClass.AliasName); layerNode.Tag = m_FeatureLayer; dsNode.Nodes.Add(layerNode); m_FeatureClass = m_EnumFC.Next(); } treeView1.Nodes.Add(dsNode); } } else if(dataSet is IFeatureClass) { //如果是FeatureClass做以下处理 IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; IFeatureClass feature = featureWorkspace.OpenFeatureClass(dataSet.Name); if (feature != null) { IFeatureLayer layer = new FeatureLayerClass(); layer.FeatureClass = feature; layer.Name = feature.AliasName; TreeNode layerNode = new TreeNode(feature.AliasName); layerNode.Tag = layer; treeView1.Nodes.Add(layerNode); } } dataSet = enumDataSet.Next(); } }