This document is archived and information here might be outdated.  Recommended version.


Create Table Snippet (ArcObjects .NET 10.4 SDK)
ArcObjects Library Reference

Create Table Snippet

Creates a dataset in a workspace.

[C#]
///<summary>Creates a table with some default fields.</summary>
/// 
///<param name="workspace">An IWorkspace2 interface</param>
///<param name="tableName">A System.String of the table name in the workspace. Example: "owners"</param>
///<param name="fields">An IFields interface or Nothing</param>
///  
///<returns>An ITable interface or Nothing</returns>
///  
///<remarks>
///Notes:
///(1) If an IFields interface is supplied for the 'fields' collection it will be used to create the
///    table. If a Nothing value is supplied for the 'fields' collection, a table will be created using 
///    default values in the method.
///(2) If a table with the supplied 'tableName' exists in the workspace an ITable will be returned.
///    if table does not exit a new one will be created.
///</remarks>
public ESRI.ArcGIS.Geodatabase.ITable CreateTable(ESRI.ArcGIS.Geodatabase.IWorkspace2 workspace, System.String tableName, ESRI.ArcGIS.Geodatabase.IFields fields)
{
  // create the behavior clasid for the featureclass
  ESRI.ArcGIS.esriSystem.UID uid=new ESRI.ArcGIS.esriSystem.UIDClass();

  if (workspace == null) return null; // valid feature workspace not passed in as an argument to the method

  ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace=(ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explicit Cast
  ESRI.ArcGIS.Geodatabase.ITable table;

  if (workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable, tableName))
  {
    // table with that name already exists return that table 
    table=featureWorkspace.OpenTable(tableName);
    return table;
  }

  uid.Value="esriGeoDatabase.Object";

  ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription=new ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();

  // if a fields collection is not passed in then supply our own
  if (fields == null)
  {
    // create the fields using the required fields method
    fields=objectClassDescription.RequiredFields;
    ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit=(ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast

    ESRI.ArcGIS.Geodatabase.IField field=new ESRI.ArcGIS.Geodatabase.FieldClass();

    // create a user defined text field
    ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEdit=(ESRI.ArcGIS.Geodatabase.IFieldEdit)field; // Explicit Cast

    // setup field properties
    fieldEdit.Name_2="SampleField";
    fieldEdit.Type_2=ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
    fieldEdit.IsNullable_2=true;
    fieldEdit.AliasName_2="Sample Field Column";
    fieldEdit.DefaultValue_2="test";
    fieldEdit.Editable_2=true;
    fieldEdit.Length_2=100;

    // add field to field collection
    fieldsEdit.AddField(field);
    fields=(ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit; // Explicit Cast
  }

  // Use IFieldChecker to create a validated fields collection.
  ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker=new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
  ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError=null;
  ESRI.ArcGIS.Geodatabase.IFields validatedFields=null;
  fieldChecker.ValidateWorkspace=(ESRI.ArcGIS.Geodatabase.IWorkspace)workspace;
  fieldChecker.Validate(fields, out enumFieldError, out validatedFields);

  // The enumFieldError enumerator can be inspected at this point to determine 
  // which fields were modified during validation.


  // create and return the table
  table=featureWorkspace.CreateTable(tableName, validatedFields, uid, null, "");

  return table;
}
[Visual Basic .NET]
'''<summary>Creates a table with some default fields.</summary>
''' 
'''<param name="workspace">An IWorkspace2 interface</param>
'''<param name="tableName">A System.String of the table name in the workspace. Example: "owners"</param>
'''<param name="fields">An IFields interface or Nothing</param>
'''  
'''<returns>An ITable interface or Nothing</returns>
'''  
'''<remarks>
'''Notes:
'''(1) If an IFields interface is supplied for the 'fields' collection it will be used to create the
'''    table. If a Nothing value is supplied for the 'fields' collection, a table will be created using 
'''    default values in the method.
'''(2) If a table with the supplied 'tableName' exists in the workspace an ITable will be returned.
'''    if table does not exit a new one will be created.
'''</remarks>
Public Function CreateTable(ByVal workspace As ESRI.ArcGIS.Geodatabase.IWorkspace2, ByVal tableName As System.String, ByVal fields As ESRI.ArcGIS.Geodatabase.IFields) As ESRI.ArcGIS.Geodatabase.ITable

  ' Create the behavior clasid for the featureclass  
  Dim uid As ESRI.ArcGIS.esriSystem.UID=New ESRI.ArcGIS.esriSystem.UIDclass

  If workspace Is Nothing Then
    Return Nothing ' valid feature workspace not passed in as an argument to the method
  End If

  Dim featureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace=CType(workspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace) ' Explicit Cast
  Dim table As ESRI.ArcGIS.Geodatabase.ITable

  If workspace.NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable, tableName) Then

    '  A table with that name already exists so return that table 
    table=featureWorkspace.OpenTable(tableName)
    Return table
  End If

  UID.Value="esriGeoDatabase.Object"

  Dim objectClassDescription As ESRI.ArcGIS.Geodatabase.IObjectClassDescription=New ESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass

  ' If a fields collection is not passed in then supply our own
  If fields Is Nothing Then

    ' Create the fields using the required fields method
    fields=objectClassDescription.RequiredFields
    Dim fieldsEdit As ESRI.ArcGIS.Geodatabase.IFieldsEdit=CType(fields, ESRI.ArcGIS.Geodatabase.IFieldsEdit) ' Explicit Cast
    Dim field As ESRI.ArcGIS.Geodatabase.IField=New ESRI.ArcGIS.Geodatabase.FieldClass

    ' Create a user defined text field
    Dim fieldEdit As ESRI.ArcGIS.Geodatabase.IFieldEdit=CType(Field, ESRI.ArcGIS.Geodatabase.IFieldEdit) ' Explicit Cast

    ' Setup field properties
    fieldEdit.Name_2="SampleField"
    fieldEdit.Type_2=ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString
    fieldEdit.IsNullable_2=True
    fieldEdit.AliasName_2="Sample Field Column"
    fieldEdit.DefaultValue_2="test"
    fieldEdit.Editable_2=True
    fieldEdit.Length_2=100

    ' Add the field to the field collection
    fieldsEdit.AddField(Field)
    fields=CType(fieldsEdit, ESRI.ArcGIS.Geodatabase.IFields) ' Explicit Cast

  End If

  ' Use IFieldChecker to create a validated fields collection.
  Dim fieldChecker As ESRI.ArcGIS.Geodatabase.IFieldChecker=New ESRI.ArcGIS.Geodatabase.FieldCheckerClass()
  Dim enumFieldError As ESRI.ArcGIS.Geodatabase.IEnumFieldError=Nothing
  Dim validatedFields As ESRI.ArcGIS.Geodatabase.IFields=Nothing
  fieldChecker.ValidateWorkspace=CType(workspace, ESRI.ArcGIS.Geodatabase.IWorkspace)
  fieldChecker.Validate(fields, enumFieldError, validatedFields)

  ' The enumFieldError enumerator can be inspected at this point to determine 
  ' which fields were modified during validation.


  ' Create and return the table
  table=featureWorkspace.CreateTable(tableName, validatedFields, uid, Nothing, "")

  Return table

End Function

Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System