In this topic
- About adding and deleting GlobalIDs
- Adding GlobalIDs to an existing feature class or table
- Adding GlobalIDs to a new feature class or table
- Deleting GlobalIDs from an existing feature class or table
About adding and deleting GlobalIDs
The geodatabase field data type, GlobalID, stores registry style strings consisting of 36-characters enclosed in curly ({}) brackets. These strings uniquely identify a feature or table row within a geodatabase and across geodatabases.
The geodatabase maintains these values automatically, much like the ObjectID field. GlobalIDs are heavily used to manage relationships, especially for replication, data management, versioning, and change-only updates. For more information, see Geodatabase field data types and Preparing data for replication.
GlobalIDs can only be added to tables or feature classes in a geodatabase (as opposed to shapefiles, coverages, and so on). Although GlobalIDs can be added to tables and feature classes with existing data in ArcSDE, to add GlobalIDs to a dataset in a personal or file geodatabase, the dataset cannot contain any rows.
Adding GlobalIDs to an existing feature class or table
The following code example uses IClassSchemaEdit3 to add a field of type GlobalID to an existing feature class or table. Use any valid string name for the GlobalID field, for example, GlobalID. As when making any type of schema change, an exclusive schema lock should be acquired for the feature class or dataset before adding or removing a GlobalID field.
[C#]
public void AddGlobalID(ITable table, String globalIdFieldName)
{
// Try to acquire an exclusive schema lock.
ISchemaLock schemaLock = (ISchemaLock)table;
try
{
schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
// Add the GlobalID field.
IClassSchemaEdit3 classSchemaEdit3 = (IClassSchemaEdit3)table;
classSchemaEdit3.AddGlobalID(globalIdFieldName);
}
catch (COMException comExc)
{
// Handle the exception appropriately for the application.
}
finally
{
// Demote the exclusive lock to a shared lock.
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
}
[VB.NET]
Public Sub AddGlobalID(ByVal table As ITable, ByVal globalIdFieldName As String)
' Try to acquire an exclusive schema lock.
Dim schemaLock As ISchemaLock = CType(table, ISchemaLock)
Try
schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock)
' Add the GlobalID field.
Dim classSchemaEdit3 As IClassSchemaEdit3 = CType(table, IClassSchemaEdit3)
classSchemaEdit3.AddGlobalID(globalIdFieldName)
Catch comExc As COMException
' Handle the exception appropriately for the application.
Finally
' Demote the exclusive lock to a shared lock.
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock)
End Try
End Sub
Adding GlobalIDs to a new feature class or table
Adding a GlobalID field (esriFieldType.esriFieldTypeGlobalID) when creating a feature class or table can be done the same way as adding any other type of field. For more information, see Creating fields.
Deleting GlobalIDs from an existing feature class or table
The following code example uses IClassSchemaEdit3 to delete the GlobalID from an existing feature class or table. ArcGIS prevents deleting the GlobalID field from a feature class or table that is participating in a replica, as replication requires the GlobalID field. In this case, unregister the replica before deleting the GlobalID field.
[C#]
public void RemoveGlobalID(ITable table)
{
// Try to acquire an exclusive schema lock.
ISchemaLock schemaLock = (ISchemaLock)table;
try
{
schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
// Remove the GlobalID field.
IClassSchemaEdit3 classSchemaEdit3 = (IClassSchemaEdit3)table;
classSchemaEdit3.DeleteGlobalID();
}
catch (COMException comExc)
{
// Handle the exception appropriately for the application.
}
finally
{
// Demote the exclusive lock to a shared lock.
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
}
}
[VB.NET]
Public Sub RemoveGlobalID(ByVal table As ITable)
' Try to acquire an exclusive schema lock.
Dim schemaLock As ISchemaLock = CType(table, ISchemaLock)
Try
schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock)
' Remove the GlobalID field.
Dim classSchemaEdit3 As IClassSchemaEdit3 = CType(table, IClassSchemaEdit3)
classSchemaEdit3.DeleteGlobalID()
Catch comExc As COMException
' Handle the exception appropriately for the application.
Finally
' Demote the exclusive lock to a shared lock.
schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock)
End Try
End Sub
See Also:
Creating fieldsHow to create a replica in a connected environment
How to create a replica in a disconnected environment
To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):
ESRI.ArcGIS.Geodatabase ESRI.ArcGIS.System (ESRI.ArcGIS.esriSystem)
Development licensing | Deployment licensing |
---|---|
ArcView | ArcView |
ArcEditor | ArcEditor |
ArcInfo | ArcInfo |
Engine Developer Kit | Engine Runtime: Geodatabase Update |