/// <summary> /// 添加新字段 /// </summary> /// <param name="fieldName">字段名称</param> /// <param name="fieldType">字段类型</param> public void AddNewFiled(string fieldName, esriFieldType fieldType) { ITable iTable = AttributeTableClass.GetITableByLayer(newFieldLayer); //先删除 DeleteFieldClass.DeleteField(iTable, fieldName); //再添加 iTable.AddField(NewFieldClass.GetNewFieldEdit(fieldName, fieldType)); AddTableColumn(fieldName, fieldName); }
public static void AddField(ITable table, string fieldName, esriFieldType fieldType, int width = -1, int precision = -1, bool nullable = true, object defaultValue = null, string aliasName = null) { if (table.FindField(fieldName) != -1) { string printName = GetPrintName(table); throw new InvalidOperationException($"Field \"{fieldName}\" already exists in {printName}."); } IField field = CreateField(fieldName, fieldType, width, precision, nullable, defaultValue, aliasName); table.AddField(field); }
/// <summary> /// 添加多个新字段 /// </summary> /// <param name="fieldNames"></param> /// <param name="fieldType">字段类型</param> public void AddNewFields(List <string> fieldNames, esriFieldType fieldType) { ITable iTable = AttributeTableClass.GetITableByLayer(newFieldLayer); foreach (string fieldName in fieldNames) { //先删除 DeleteFieldClass.DeleteField(iTable, fieldName); //再添加 iTable.AddField(NewFieldClass.GetNewFieldEdit(fieldName, fieldType)); } AddTableColumns(fieldNames); }
/// <summary> /// 向feature添加字段 /// </summary> /// <param name="layer"></param> /// <param name="fieldName"></param> /// <param name="AliasName"></param> /// <param name="length"></param> /// <returns></returns> static public bool AddField(IFeatureClass layer, string fieldName, string AliasName, int length) { try { ITable pTable = (ITable)layer; IFieldEdit pFieldEdit = new FieldClass() { IFieldEdit_Type_2 = esriFieldType.esriFieldTypeString, IFieldEdit_AliasName_2 = AliasName, IFieldEdit2_Name_2 = fieldName, IFieldEdit_Length_2 = 50, IFieldEdit_IsNullable_2 = true }; pTable.AddField(pFieldEdit); Console.WriteLine("添加字段成功"); return(true); } catch (Exception ex) { return(false); } }
public static void addField(ITable pTable, string FieldName, ESRI.ArcGIS.Geodatabase.esriFieldType FieldType, int FieldLength, int FieldPrecision, int FieldScale = 0) { // Error Handling const string functionName = "addField"; IField pField = new Field(); IFieldEdit pFieldEdit = pField as IFieldEdit; try { if (pTable.FindField(FieldName) < 0) { pFieldEdit.Name_2 = FieldName; pFieldEdit.Type_2 = FieldType; pFieldEdit.Length_2 = FieldLength; if (FieldType != ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString) { pFieldEdit.Precision_2 = FieldPrecision; pFieldEdit.Scale_2 = FieldScale; } pField = pFieldEdit as IField; pTable.AddField(pField); } } catch (Exception e) { MessageBox.Show(functionName + " :: " + e.Message, "SF10", MessageBoxButtons.OK, MessageBoxIcon.Error); } finally { pField = null; pFieldEdit = null; } }
public static bool bAddFields(ITable pTable, IFields2 pNewFields) { // Error Handling const string functionName = "bAddFields"; try { int i; IField pField; string sFieldName; for (i = 0; i < pNewFields.FieldCount; i++) { pField = pNewFields.Field[i]; sFieldName = pField.Name; IFieldEdit pFldEdit; if (pTable.FindField(sFieldName) < 0 && pField.Type != esriFieldType.esriFieldTypeOID && pField.Type != esriFieldType.esriFieldTypeGUID) { pFldEdit = (IFieldEdit)pField; pFldEdit.Editable_2 = true; pTable.AddField(pField); } } return true; } catch (Exception e) { MessageBox.Show(functionName + " :: " + e.Message, "SF10", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } }
public string createField(ITable inFtr, string nm, esriFieldType FieldType,bool renameFldName=true) { string snm = getSafeFieldName(inFtr, nm); bool fieldWasRenamed = false; if(snm.StartsWith("_"))fieldWasRenamed=true; bool checkSettings = true; if(fieldWasRenamed&&!renameFldName) { checkSettings=false; snm = nm; } if (checkSettings) { ISchemaLock schemaLock = (ISchemaLock)inFtr; try { // A try block is necessary, as an exclusive lock might not be available. schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); IField fld = new FieldClass(); IFieldEdit fldE = (IFieldEdit)fld; fldE.AliasName_2 = nm; fldE.Name_2 = snm; fldE.Type_2 = FieldType; if (FieldType == esriFieldType.esriFieldTypeSmallInteger || FieldType == esriFieldType.esriFieldTypeSingle || FieldType == esriFieldType.esriFieldTypeInteger || FieldType == esriFieldType.esriFieldTypeDouble) { fldE.DefaultValue_2 = 0; } inFtr.AddField(fldE); } catch (Exception exc) { // Handle appropriately for your application. Console.WriteLine(exc.Message); //MessageBox.Show(exc.Message); } finally { // Set the lock to shared, whether or not an error occurred. schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } } return snm; }