在为现有要素类新增字段时,有可能会爆出该表正被其他程序或其他人使用,这时我们就会添加失败,解决办法如下:

在新增字段之前,先修改SchemaLock的状态

ISchemaLock pSchemaLock = m_pOriginFeatureClass as ISchemaLock;
pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);

然后再新增字段

//Add New Field
int nIndexOfGBTYPE = m_pOriginFeatureClass.Fields.FindField("GB_TYPE");
if (nIndexOfGBTYPE == -1)
{
IField pField = new FieldClass();
IFieldEdit pFieldEdit = pField as IFieldEdit;
pFieldEdit.Name_2 = "GB_TYPE";
pFieldEdit.Type_2 = esriFieldType.esriFieldTypeString;
pFieldEdit.IsNullable_2 = true;
pFieldEdit.AliasName_2 = "GB_TYPE";
pFieldEdit.DefaultValue_2 = "";
pFieldEdit.Editable_2 = true;
pFieldEdit.Length_2 = 100;

m_pOriginFeatureClass.AddField(pField);
}

//最后进行恢复处理

pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);

05-06 07:19