创建FeatureClass方法、创建要素类出错、FeatureWorkspace.CreateFeatureClass
转载地址:http://blog.csdn.net/loveyou388i/article/details/53519718
原标题: .CreateFeatureClass报错原因解析
声明:本博客仅对博主一人负责,不保证其正确性级准确性,仅供参考,欢迎指正。
1.Microsoft Jet 数据库引擎找不到输入表或查询 ‘GDB_DataChanges’。 确定它是否存在,以及它的名称的拼写是否正确。
我发现在百度上是找不到关于这个几个关键字的信息的,因此本菜鸟翻越长城,找到了解决办法,详细见下。我没有仔细看下面说明,因为英文实在是憋足。但我估计是9.3和10.0的AE开发版本不同,应该是老版本需要使用IWorkspaceEdit的StarEditing()方法和StopEditing()方法。
主要解决办法就是通过IWorkspaceEdit的StarEditing()方法和StopEditing()方法,在两个方法运行中间进行CreateFeatureClass。
原地址:http://gis.stackexchange.com/questions/103949/create-featureclass-in-personal-geodatabase-and-store-data
原因说明:https://geonet.esri.com/thread/45118
下面是个从无空间定义的mdb数据转换成有空间数据的FeatureClass的例子,错误字段在IEnumFieldsError 类中保存,遍历可以获取错误信息。
private IFeatureClass CreateFeatureClass(IFeatureWorkspace pfws)
{
if (pDemoFeatureClass == null) return null;
List<NewFieldClass> lFields = pDemoFeatureClass.Fields;
IFeatureWorkspace pFeatureWorkspace = pfws;
IFields pFields = new FieldsClass();
IFieldsEdit pFieldsEdit = pFields as IFieldsEdit;
IFieldEdit pFieldEdit = new FieldClass();
#region esriFieldType
foreach (NewFieldClass nField in lFields)
{
pFieldEdit = new FieldClass();
if (nField.FieldType == 0||nField.FieldType == 4)
continue;
pFieldEdit.Name_2 = nField.Name;
pFieldEdit.AliasName_2 = nField.AliasName;
switch (nField.FieldType)
{
case 0: break;
case 1: pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString; break;
case 2: pFieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble; break;
case 3: pFieldEdit.Type_2 = esriFieldType.esriFieldTypeInteger; break;
case 4: pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry; break;
default: break;
}
switch (nField.IsNullable)
{
case 0: pFieldEdit.IsNullable_2 = false; break;
case 1: pFieldEdit.IsNullable_2 = true; break;
default: pFieldEdit.IsNullable_2 = true; break;
}
//switch (nField.Check)
//{
// case 0: break;
// case 1: break;
// default: break;
//}
pFieldEdit.Length_2 = nField.FieldLength;
pFieldsEdit.AddField(pFieldEdit as IField);
}
#endregion
//设置空间属性字段
//Domain
ISpatialReference pSpatialReference = new UnknownCoordinateSystemClass();
pSpatialReference.SetDomain(-99999999, 99999999, -99999999, 99999999);
pSpatialReference.SetZDomain(-99999999, 99999999);
pSpatialReference.SetMDomain(-99999999, 99999999);
//空间字段设计
pFieldEdit = new FieldClass();
pFieldEdit.Name_2 = "SHAPE";
pFieldEdit.AliasName_2 = "SHAPE";
IGeometryDef pGDef = new GeometryDefClass();
IGeometryDefEdit pGDefEdit = pGDef as IGeometryDefEdit;
string GeoType = pDemoFeatureClass.Name.Substring(pDemoFeatureClass.Name.LastIndexOf("_")).ToUpper();
if (GeoType.Contains("_L"))
pGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
else if (GeoType.Contains("_P"))
pGDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;
pGDefEdit.SpatialReference_2 = pSpatialReference;
//设置空坐标系↓
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;
pFieldEdit.GeometryDef_2 = pGDef;
pFieldsEdit.AddField(pFieldEdit as IField);
pFieldEdit.GeometryDef_2 = pGDef;
//创建一个Validated Fields集合
IFieldChecker fieldChecker = new FieldCheckerClass();
IEnumFieldError enumFieldError = null;
IFields validatedFields = null;
fieldChecker.ValidateWorkspace = (IWorkspace)pfws;
fieldChecker.Validate(pFields, out enumFieldError, out validatedFields);
if (enumFieldError != null)
{
IFieldError pError = enumFieldError.Next();
while (pError != null)
{
pError.FieldError.ToString();
pError.FieldIndex.ToString();
pError = enumFieldError.Next();
}
}
UID pUidClsId = new UIDClass();
UID pUidClsExt = new UIDClass();
esriFeatureType pFeatureType = esriFeatureType.esriFTSimple;
#region pUidClsID字段为空时
if (pUidClsId == null)
{
pUidClsId = new UIDClass();
switch (pFeatureType)
{
case (esriFeatureType.esriFTSimple):
//if (pGeometryType == esriGeometryType.esriGeometryLine)
// pGeometryType = esriGeometryType.esriGeometryPolyline;
pUidClsId.Value = "{52353152-891A-11D0-BEC6-00805F7C4268}";
break;
case (esriFeatureType.esriFTSimpleJunction):
//pGeometryType = esriGeometryType.esriGeometryPoint;
pUidClsId.Value = "{CEE8D6B8-55FE-11D1-AE55-0000F80372B4}";
break;
case (esriFeatureType.esriFTComplexJunction):
pUidClsId.Value = "{DF9D71F4-DA32-11D1-AEBA-0000F80372B4}";
break;
case (esriFeatureType.esriFTSimpleEdge):
//pGeometryType = esriGeometryType.esriGeometryPolyline;
pUidClsId.Value = "{E7031C90-55FE-11D1-AE55-0000F80372B4}";
break;
case (esriFeatureType.esriFTComplexEdge):
//pGeometryType = esriGeometryType.esriGeometryPolyline;
pUidClsId.Value = "{A30E8A2A-C50B-11D1-AEA9-0000F80372B4}";
break;
case (esriFeatureType.esriFTAnnotation):
//pGeometryType = esriGeometryType.esriGeometryPolygon;
pUidClsId.Value = "{E3676993-C682-11D2-8A2A-006097AFF44E}";
break;
case (esriFeatureType.esriFTDimension):
//pGeometryType = esriGeometryType.esriGeometryPolygon;
pUidClsId.Value = "{496764FC-E0C9-11D3-80CE-00C04F601565}";
break;
}
}
#endregion
#region pUidClsExt字段为空时
if (pUidClsExt == null)
{
switch (pFeatureType)
{
case esriFeatureType.esriFTAnnotation:
pUidClsExt = new UIDClass();
pUidClsExt.Value = "{24429589-D711-11D2-9F41-00C04F6BC6A5}";
break;
case esriFeatureType.esriFTDimension:
pUidClsExt = new UIDClass();
pUidClsExt.Value = "{48F935E2-DA66-11D3-80CE-00C04F601565}";
break;
}
}
#endregion
string pNewFeatureClassName = pDemoFeatureClass.Name.Split('.')[1];
try
{
IFeatureClass pFeatureClass = pFeatureWorkspace.CreateFeatureClass(pNewFeatureClassName,pFields,pUidClsId,pUidClsExt,esriFeatureType.esriFTSimple,"SHAPE",String.Empty);
if (pFeatureClass != null)
return pFeatureClass;
else return null;
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
return null;
}
}
其实在10.0版本以后,似乎只要Fields数组参数格式标准了(有OID和SHAPE包含GeometryDef)字段后,就不会报错,因此百度上也很少见到这个错误。CreateFeatureClass报错基本都是Fields参数出错。
2.设置GeometryDef_2时,提示 对 COM 组件的调用返回了错误 HRESULT E_FAIL 错误。如下图示:
我找很多教程,发现都不是针对这个的,无意中看见了博客
http://www.cnblogs.com/qiushuixizhao/p/3242685.html 中的写法, 发现别人是先AddField之后再定义这个GeometryDef_2的,因此我改正过顺序,变成:先添加之后再定义GeometryDef_2,发现程序正常运行了。