C#Arcengine不使用gp服务,在GDB数据库中新建一个表
时间: 2024-03-11 10:48:57 浏览: 64
您好!要在C#ArcEngine中不使用GP服务来创建一个表,需要使用工作空间和数据集来完成。以下是示例代码:
```csharp
//获取工作空间并创建一个数据集
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory();
IWorkspace workspace = workspaceFactory.OpenFromFile(workspacePath, 0);
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;
IWorkspace2 workspace2 = (IWorkspace2)workspace;
IFeatureDataset featureDataset = featureWorkspace.CreateFeatureDataset(datasetName, spatialReference);
//在数据集中创建一个新表格
IFields fields = new Fields();
IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
IField idField = new Field();
IFieldEdit2 idFieldEdit = (IFieldEdit2)idField;
idFieldEdit.Name_2 = "ID";
idFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger;
idFieldEdit.IsNullable_2 = false;
idFieldEdit.Required_2 = true;
fieldsEdit.AddField(idField);
IField nameField = new Field();
IFieldEdit2 nameFieldEdit = (IFieldEdit2)nameField;
nameFieldEdit.Name_2 = "Name";
nameFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
nameFieldEdit.Length_2 = 50;
nameFieldEdit.IsNullable_2 = false;
nameFieldEdit.Required_2 = true;
fieldsEdit.AddField(nameField);
IField shapeField = new Field();
IFieldEdit2 shapeFieldEdit = (IFieldEdit2)shapeField;
shapeFieldEdit.Name_2 = "Shape";
shapeFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
IGeometryDef geometryDef = new GeometryDef();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;
geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
geometryDefEdit.SpatialReference_2 = spatialReference;
shapeFieldEdit.GeometryDef_2 = geometryDef;
fieldsEdit.AddField(shapeField);
IFeatureClass featureClass = featureDataset.CreateFeatureClass(tableName, fields, null, null, esriFeatureType.esriFTSimple, "Shape", "");
```
其中,workspacePath是工作空间的路径,datasetName是数据集的名称,tableName是新表格的名称,spatialReference是空间参考。创建新表格时,需要指定表格的字段,包括ID、名称和形状等属性。执行该代码后,将会在指定的工作空间中创建一个新的表格。
阅读全文