//注: 在程序运行中往往有些要素类因为出错而无法删除,这时IDataset.Delete这个方法则无法成功,可以用轻量级的名称对象来完成这个任务。以下为C#+AE10源码。http://blog.csdn.net/lysc_forever/article/details/7790219
//删除要素类
public static bool DeleteFeatureDataset(IWorkspace ws, string name)
{
if (ws == null || string.IsNullOrEmpty(name))
{
ErrMsg = "工作空间或要素类名称不能为空!";
return false;
}
IFeatureDataset pFeaDataSet;
IEnumDatasetName pEnumDatasetName;
IFeatureWorkspace pFeaWorkspace;
IDatasetName pDatasetName;
try
{
pFeaWorkspace = ws as IFeatureWorkspace;
pEnumDatasetName = ws.get_DatasetNames(esriDatasetType.esriDTFeatureClass ^ esriDatasetType.esriDTFeatureDataset);
pEnumDatasetName.Reset();
pDatasetName = pEnumDatasetName.Next();
while (pDatasetName != null)
{
if (pDatasetName.Type == esriDatasetType.esriDTFeatureDataset)
{
//如果是要素集,则对要素集内的要素类进行查找
IEnumDatasetName pEnumFcName = (pDatasetName as IFeatureDatasetName).FeatureClassNames;
IDatasetName pFcName = pEnumFcName.Next();
while (pFcName != null)
{
if (pFcName.Name.IndexOf(name) >= 0)
{
DeleteByName(pFeaWorkspace, pFcName);
return true;
}
pFcName = pEnumFcName.Next();
}
}
else
{
if (pDatasetName.Name.IndexOf(name) >= 0)
{
DeleteByName(pFeaWorkspace, pDatasetName);
return true;
}
}
pDatasetName = pEnumDatasetName.Next();
}
return false;
}
catch (Exception ex)
{
ErrMsg = ex.Message;
return false;
}
}
//删除名称对象
public static void DeleteByName(IFeatureWorkspace pFeaWorkspace, IDatasetName pDatasetName)
{
IFeatureWorkspaceManage pWorkspaceManager = pFeaWorkspace as IFeatureWorkspaceManage;
pWorkspaceManager.DeleteByName(pDatasetName);
}
其实有时候往往因为sde中的表被锁,而无法删除要素,感觉最好的解决办法是删除记录,而不是删除表。
具体操作如下:
(featureclass as ITable).DeleteSearchedRows(null);