shp导入Sde
private void exportSdeBtn_Click(object sender, EventArgs e)
{
OpenFileDialog penFileDialog1 = new OpenFileDialog();
penFileDialog1.Filter = ("ShapeFile 文件(*.shp)|*.shp");
penFileDialog1.InitialDirectory = "D:\\project\\ThematicDataManager";//对话框目录
penFileDialog1.ShowDialog();
string outLyerPath = penFileDialog1.FileName;
string layerDirectoryName = Path.GetDirectoryName(outLyerPath)+"\\";
string LayerName = Path.GetFileNameWithoutExtension(outLyerPath);
ShapefileWorkspaceFactory fileWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
IWorkspace featureWorkspce = fileWorkspaceFactory.OpenFromFile(layerDirectoryName, 0);
string ServerName = string.Empty;
string instance = string.Empty;
string UserName = string.Empty;
string PassWord = string.Empty;
string SDEVersion = string.Empty;
ServerName = "192.168.3.108";
instance = "port:5151";
UserName = "sde";
PassWord = "sde";
SDEVersion = "SDE.DEFAULT";
IPropertySet pOutSDEPropset;
pOutSDEPropset = new PropertySet();
pOutSDEPropset.SetProperty("SERVER", ServerName);
pOutSDEPropset.SetProperty("INSTANCE", instance);
pOutSDEPropset.SetProperty("USER", UserName);
pOutSDEPropset.SetProperty("Password", PassWord);
pOutSDEPropset.SetProperty("version", SDEVersion);
//获取空间数据库对象的管理器
IWorkspaceFactory pWorkFact;
//这句话貌似根据数据库类型不同new的方法不一样.
//sde的是new 的SdeWorkspaceFactoryClass.
pWorkFact = new SdeWorkspaceFactoryClass();
IWorkspace pOutSDEWorkspace;
pOutSDEWorkspace = pWorkFact.Open(pOutSDEPropset, 0);
IFeatureDataConverter_ConvertFeatureClass(featureWorkspce, pOutSDEWorkspace, LayerName, LayerName);
}
/// <summary>
/// 将一个要素类从一个工作空间转移到另外一个工作空间
/// 注意目标工作空间不能有改要素类,必须先清除
/// </summary>
/// <param name="sourceWorkspace">源工作空间</param>
/// <param name="targetWorkspace">目标工作空间</param>
/// <param name="nameOfSourceFeatureClass">源要素类名</param>
/// <param name="nameOfTargetFeatureClass">目标要素类名</param>
public void IFeatureDataConverter_ConvertFeatureClass(IWorkspace sourceWorkspace, IWorkspace targetWorkspace, string nameOfSourceFeatureClass, string nameOfTargetFeatureClass)
{
//create source workspace name
IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
//create source dataset name
IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
sourceDatasetName.WorkspaceName = sourceWorkspaceName;
sourceDatasetName.Name = nameOfSourceFeatureClass;
//create target workspace name
IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
//create target dataset name
IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
targetDatasetName.WorkspaceName = targetWorkspaceName;
targetDatasetName.Name = nameOfTargetFeatureClass;
//Open input Featureclass to get field definitions.
ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();
//Validate the field names because you are converting between different workspace types.
IFieldChecker fieldChecker = new FieldCheckerClass();
IFields targetFeatureClassFields;
IFields sourceFeatureClassFields = sourceFeatureClass.Fields;
IEnumFieldError enumFieldError;
// Most importantly set the input and validate workspaces!
fieldChecker.InputWorkspace = sourceWorkspace;
fieldChecker.ValidateWorkspace = targetWorkspace;
fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError, out targetFeatureClassFields);
// Loop through the output fields to find the geomerty field
IField geometryField;
for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
{
if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
{
geometryField = targetFeatureClassFields.get_Field(i);
// Get the geometry field's geometry defenition
IGeometryDef geometryDef = geometryField.GeometryDef;
//Give the geometry definition a spatial index grid count and grid size
IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
targetFCGeoDefEdit.GridCount_2 = 1;
targetFCGeoDefEdit.set_GridSize(0, 0);
//Allow ArcGIS to determine a valid grid size for the data loaded
targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
// we want to convert all of the features
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = "";
// Load the feature class
IFeatureDataConverter fctofc = new FeatureDataConverterClass();
IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName, queryFilter, null, targetFeatureClassName, geometryDef, targetFeatureClassFields, "", 1000, 0);
break;
}
}