Data


Additional assembly information: Contents, Object Model Diagram
The Data namespace contains classes to access data stored on disk. It provides the capability for you to determine what is stored inside a geodatabase or directory, and to be able to access and query that data. For example, you can open a geodatabase table and perform a query on it so that only the rows that satisfy the search criteria are returned.  This namespace does not provide capabilities to manage or edit data; therefore, you will not be able to create or modify data. The Data namespace has linkages to both the Mapping and Geometry namespaces. For example, you can return the data source object associated with a layer on a map or you can access the actual geometry stored in a row of data.

See the following sections for more information about this namespace:

Connecting to a data store

  • Geodatabase and DataDirectory classes—To open a connection to a geodatabase, create an instance of the Geodatabase class. For data held in directories, create an instance of the DataDirectory class. You can explore the contents of a geodatabase or directory using the GetTables and the GetRasters methods, which both return strongly typed collections. 

    The following code example opens the Scotland geodatabase, then prints the names of all feature classes contained within it:
[C#]
Geodatabase gdb = new Geodatabase(@"C:\Data\Scotland.gdb");
foreach (Table table in gdb.GetTables(TableDiscoveryOptions.Spatial))
{
    Debug.Print(table.Name);
}
[VB.NET]
Dim gdb As Geodatabase = New Geodatabase("C:\Data\Scotland.gdb")
For Each table As Table In gdb.GetTables(TableDiscoveryOptions.Spatial)
    Debug.Print(table.Name)
Next
By using the DataDirectory class, you can open shapefiles, dBase files, and any ArcGIS Explorer supported raster format.
Geodatabase data is often logically grouped into feature datasets and the data namespace represents these using the GeodatabaseFolder class. You can return a collection of all feature datasets in a geodatabase using the GetGeodatabaseFolders method. 

Accessing vector data

  • Table, Column and Row classes—The Table class represents non-spatial or vector data. Tables can be opened from a geodatabase or DataDirectory object using the Geodatabase.OpenTable or DataDirectory.OpenTable methods, respectively.

    Alternatively, use the static Open method on the Table class. The IsSpatial property indicates whether the table can contain spatial data and the GeometryType property returns the type of vectors it can store. 
If you want to open a Table to create a Layer, consider using one of the convenience methods available on the FeatureLayer class instead.
A table is defined by its constituent columns and can be accessed using the Table.Columns property. Individual Column objects can be returned from the ColumnCollection by name, index position, or by iterating over the collection. The data contained within a table is stored in rows. You can access the data in a table using the Table.GetRows method to return a RowCollection, which contains Row objects. You can then return the value of the data stored in each column using the Values property. The Row class represents spatial and non-spatial data; therefore, a convenience property called Geometry has been added to provide easy access to the vector data stored in the geometry column.  
The following code example shows the mountains table opened and the name of each mountain printed:
[C#]
Table mountains = gdb.OpenTable("mountains");
foreach (Row row in mountains.GetRows())
{
    //Print the name of the mountain stored in the Name column.
    Debug.Print(row.Values["Name"]);
}
[VB.NET]
Dim mountains As Table = gdb.OpenTable("mountains")
For Each row As Row In mountains.GetRows()
    'Print the name of the mountain stored in the Name column.
    Debug.Print(row.Values.Item("Name"))
Next
There are syntax differences between C# and VB.NET when accessing values in a RowValueCollection. In C#, use the indexer (using [] syntax) whereas in VB.NET, use the Item property.
  • Binding a table to a .NET control—In a typical database driven, Windows forms application, linking a data source to a control is easy using .NET data binding. For example, the common scenario of creating a table attribute viewer can be achieved by binding a DataGridView control to a database table at design time. At run time, the DataGridView control will be populated with data from the table. This same scenario can be achieved for geodatabase tables, feature classes, shapefiles, and dBase tables using the TableBindingAdapter class.
  • Performing table queries—Executing queries on tables is a powerful mechanism for performing data analysis and this is possible using the Table.Search method in conjunction with the Filter class. To execute a query, create a Filter object to define the search criteria and pass it into the Search method. Results are returned as a RowCollection object. For non-spatial queries, assign a Structured Query Language (SQL) clause in the WhereClause property and for spatial queries, specify a search geometry and spatial relationship using the Geometry and SpatialSearchType properties, respectively. 

    The following code example returns only those mountains that are classified as being Munros (higher than 3000ft):
[C#]
//Find only those rows that have a Type column value of 'Munro'.
RowCollection rows = mountains.Search(new Filter("Type = 'Munro'"));
[VB.NET]
'Find only those rows which have a Type column value of 'Munro'.
Dim rows As RowCollection = mountains.Search(New Filter("Type = 'Munro'"))
It is also possible to perform proximity based searches using the Table.SearchByDistance method. For example, you could find all the schools within five miles of a clicked location on the map.
  • Improving table performance (Index class)—Achieving optimum query performance is largely governed by whether indexes exist on the relevant columns. The Table class provides access to its indexes via the Indexes property. Importantly, you have the ability to create indexes by creating an Index object and adding it to the IndexCollection. Alternatively, there is a convenience method called CreateIndex on the Column class that can also be used to create a single column index.
You cannot create or delete indexes on tables stored in  ArcSDE geodatabases.
You cannot modify the properties of an existing index.
  • Domain and Subtypes classesDomains are typically used to enforce data integrity when editing, as well as providing traditional database lookup functionality (column values are stored as integers but looked up as strings). The data namespace represents these geodatabase entities with the Domain, CodedValueDomain, and RangeDomain classes. The Column.HasDomain property indicates whether a column has a domain defined against it and the Column.GetDomain method returns the actual domain object. Additionally, for a particular row value, it is easy to perform a domain lookup using the RowValueCollection.GetCodedName method. See the following code example: 
[C#]
string lookupDomainValue = row.Values.GetCodedName("TypeAsDomain");
[VB.NET]
Dim lookupDomainValue As String = row.Values.GetCodedName("TypeAsDomain")
Multiple datasets that share significant commonality (for example, same table schema) but have some differences (for example, different range of valid column values) are often stored in a single geodatabase table or feature class as subtypes.
For example, a roads feature class could be categorized using the "RoadType" column into four subtypes: interstate highways, primary roads, secondary roads, and ramps and interchanges. Each remaining column can also have different domains and default values defined for each subtype. The  "Composition" column can have a different attribute domain assigned to each subtype, which specifies a list of possible construction materials. For highways, the valid domain values can be concrete and asphalt whereas for secondary roads, the values can be concrete, asphalt, brick, cobbles, and gravel.
You can check whether a Table has been subtyped using the IsSubtyped property and use the ColumnCollection.Subtypes property to return the subtypes defined on a particular column. To retrieve a domain for a particular subtype, use one of the Column.GetDomain methods.
Domains and subtypes cannot be created or modified using the classes in the Data namespace. 
  • Working with relationships and joins (TableRelationship class)—The Data namespace supports geodatabase relationships. You can find out about the relationships that a table participates in, and for a particular row or rows, return the related rows in another table. You cannot create relationship classes in a geodatabase, but you can create in-memory relationships or join two tables using a shared column.

    The HasRelationships property indicates whether a table participates in any relationships and they can be accessed using the GetTableRelationships method. 

    The Data namespace makes it easy to find related data using the GetRelatedRows method available on the RowCollection and Row classes. The following screen shot shows a one-to-many relationship between the mountains and mtn_ratings tables. On the screen shot, Ben Lomond has three ratings: two excellent and one poor. 

The following code example shows how the related rows in the mtn_ratings can be accessed from the row in the mountains table.
[C#]
RowCollection relatedRows = row.GetRelatedRows("MountainRatings");
[VB.NET]
Dim relatedRows As RowCollection = row.GetRelatedRows("MountainRatings")
To create a new in-memory relationship between two tables, use the static CreateMemoryRelationship method to return a new instance of the TableRelationship class. 
To perform a join between two tables, you can use the TableRelationship.CreateJoinedTable method or the Table.Join method. The following screen shot shows the result of joining the regions table with the region_stats table:
The following code example shows how to join the regionStats table with the regions table:
[C#]
Table joinedTable = regions.Join(regionStats, "OBJECTID", "REGIONID");
[VB.NET]
Dim joinedTable As Table = regions.Join(regionStats, "OBJECTID", "REGIONID")

Accessing raster data

The Raster class represents any data held in a raster format. Opening a raster follows exactly the same pattern as opening tables; therefore, there is an instance method, called OpenRaster, on the Geodatabase and DataDirectory classes and a static convenience method called Open on the Raster class.
If you want to open a raster to create a layer, consider using one of the convenience methods available on the RasterLayer class instead.
A common scenario when working with raster datasets is to find a cell value at the mouse-clicked location (this is possible using the GetValueAtPoint method).
Raster display performance can be optimized by ensuring that pyramids have been built for each dataset.  While increasing the size of a raster, pyramids also improve the display speed by only retrieving the necessary data at a particular resolution. Use the HasPyramids property to check for their existence and the BuildPyamids method to create them.
You can build pyramids on rasters stored in file geodatabases and in directories but not rasters stored in  ArcSDE geodatabases.