private static void UpdateCodedValueDomain(IWorkspace theWorkspace, string DomainName, string SourceClassName, string CodeFieldName, string ValueFieldName) { // Get reference to the table to read codes and values from ITable theTable = commonFunctions.OpenTable(theWorkspace, SourceClassName); // Get reference to the domain itself IWorkspaceDomains wsDomains = (IWorkspaceDomains)theWorkspace; ICodedValueDomain theDomain = (ICodedValueDomain)wsDomains.get_DomainByName(DomainName); // Requires exclusive schema lock ISchemaLock schemaLock = (ISchemaLock)theDomain; try { // Get an exclusive lock schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock); // Clear everything out of the domain first int codedValues = theDomain.CodeCount; for (int i = 0; i <= codedValues - 1; i++) { theDomain.DeleteCode(theDomain.Value[0]); } // Sort the table ITableSort tableSorter = new TableSortClass(); tableSorter.Fields = ValueFieldName; tableSorter.set_Ascending(ValueFieldName, true); tableSorter.Table = theTable; tableSorter.Sort(null); // Loop through the sorted rows, add to the domain int codeFld = theTable.FindField(CodeFieldName); int valueFld = theTable.FindField(ValueFieldName); ICursor theCursor = tableSorter.Rows; IRow theRow = theCursor.NextRow(); while (theRow != null) { theDomain.AddCode(theRow.get_Value(codeFld), theRow.get_Value(valueFld).ToString()); theRow = theCursor.NextRow(); } } catch (Exception e) { MessageBox.Show(DomainName + " was not updated. This is likely because an exclusive schema lock could not be obtained.", "NCGMP Tools"); } finally { // Release the exclusive lock, if it was acquired. // This block of code (finally) is called whether or not there is a problem schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock); } }
private static IDomain GetOrCreateDomain(IWorkspace ws) { IWorkspaceDomains wsd = ws as IWorkspaceDomains; IDomain domain = wsd.get_DomainByName(DOMAIN_NAME); if (domain == null) { ICodedValueDomain2 cvd = new CodedValueDomain() as ICodedValueDomain2; cvd.AddCode(1, "New Feature"); cvd.AddCode(2, "Modified Feature"); cvd.AddCode(3, "Modified Feature (After)"); cvd.AddCode(6, "Modified Feature (Before)"); cvd.AddCode(4, "Deleted Feature"); domain = cvd as IDomain; domain.FieldType = esriFieldType.esriFieldTypeInteger; domain.Name = DOMAIN_NAME; domain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate; domain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue; int i = wsd.AddDomain(domain); } return(domain); }