To use the code in this topic, reference the following namespaces via the using (C#) or Imports (VB .NET) statements. It is also necessary to add the corresponding references to the project to gain access to these application programming interfaces (APIs).
A workspace is a container of spatial and nonspatial datasets, such as feature classes, raster datasets, and tables. Workspaces provide methods to access existing datasets and create new datasets.
The following are the three types of workspaces (represented by values in the esriWorkspaceType enumeration):
Shapefile workspaces and ArcInfo workspaces are examples of file-based workspaces (esriFileSystemWorkspace).
A personal geodatabase stored in a Microsoft Access or a file geodatabase are examples of local database workspaces (esriLocalDatabaseWorkspace).
An ArcSDE geodatabase stored in a relational database management system (RDBMS) (such as Oracle, DB2, SQL Server, or Informix) is a remote database workspace (esriRemoteDatabaseWorkspace).
There are several methods that can be used to open a geodatabase workspace, each with its own purpose. This document reviews these methods for different types of workspaces and discusses when one can offer advantages over another.
To open a workspace, an appropriate workspace factory must be created. Each workspace type has its own workspace factory.
A WorkspaceFactory is a dispenser of workspaces and allows a client to connect to a workspace specified by a set of connection properties. A WorkspaceFactory is a cocreatable, singleton object. A singleton object can only be instantiated once in a process. The WorkspaceFactory maintains a pool of currently connected, active workspaces that are referenced by the application. The workspace factory classes for geodatabase workspaces are found in the DataSourcesGDB library.
Methods for connecting to a workspace
The following are the three different ways to open a workspace after creating the appropriate workspace factory:
IWorkspaceFactory.Open—The Open method takes as input a set of connection properties that specify the workspace to establish a connection with.
IWorkspaceFactory.OpenFromFile—The OpenFromFile method takes the path name of a file or directory that represents an esriFileSystemWorkspace, an esriLocalDatabaseWorkspace, or a connection file to an esriRemoteDatabaseWorkspace and returns the specified workspace.
IWorkspaceFactory2.OpenFromString—The OpenFromString method on the IWorkspaceFactory2 interface was added to allow the opening of a workspace using a connection string that describes the connection properties for the workspace. The connection string is a collection of name value pairs separated by semicolons (;).
Connecting to a personal geodatabase workspace stored in Access
The workspace factory required to connect to a personal geodatabase is an AccessWorkspaceFactory. For local database workspaces, the IWorkspaceFactory.Open method usually requires a property set with a single property named DATABASE, the value of which should be the path to the workspace (including the filename).
The following code example accepts a string that is used to populate the required property to open and return a personal geodatabase workspace with the Open method:
[C#]
// For example, database = "C:\\myData\\mypGDB.mdb".public IWorkspace AccessWorkspaceFromPropertySet(string database)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("DATABASE", database);
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
[VB.NET]
' For example, database = "C:\myData\mypGDB.mdb".PublicFunction AccessWorkspaceFromPropertySet(ByVal database AsString) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("DATABASE", database)
Dim workspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactoryClass()
Return workspaceFactory.Open(propertySet, 0)
EndFunction
The OpenFromFile method for personal geodatabases requires the path name of the .mdb file that stores the Access database.
The following code example accepts the same string as the Open function, except it passes it directly into the OpenFromFile method instead of using it to create a property set. OpenFromFile is the most commonly used method to open personal geodatabases.
[C#]
// For example, path = "C:\\myData\\mypGDB.mdb".public IWorkspace AccessWorkspaceFromPath(string path)
{
IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
return workspaceFactory.OpenFromFile(path, 0);
}
[VB.NET]
' For example, path = "C:\myData\mypGDB.mdb".PublicFunction AccessWorkspaceFromPath(ByVal Path AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory = New AccessWorkspaceFactoryClass()
Return workspaceFactory.OpenFromFile(Path, 0)
EndFunction
The last way to open a personal geodatabase is to utilize the OpenFromString method on IWorkspaceFactory2. The connection string used in this case is a collection of the name value pairs. For personal geodatabases, the collection only requires a name of DATABASE and a value of the path name to the workspace. See the following code example:
[C#]
// For example, connectionString = "DATABASE=C:\\myData\\mypGDB.mdb".public IWorkspace AccessWorkspaceFromString(string connectionString)
{
IWorkspaceFactory2 workspaceFactory = new AccessWorkspaceFactoryClass();
return workspaceFactory.OpenFromString(connectionString, 0);
}
[VB.NET]
' For example, connectionString = "DATABASE=C:\myData\mypGDB.mdb".PublicFunction AccessWorkspaceFromString(ByVal connectionString AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory2 = New AccessWorkspaceFactoryClass()
Return workspaceFactory.OpenFromString(connectionString, 0)
EndFunction
The required code to connect to a file geodatabase is almost identical to the code for connecting to a personal geodatabase. Both are local database workspaces, but as stated previously, a different type of workspace factory is required. Additionally, the extension used on the database will be different. For a file geodatabase, the extension is .gdb.
The following code example uses the Open method to return a file geodatabase workspace:
The only differences between file and personal geodatabase connections are the workspace factory and the extension of the database.
[C#]
// For example, database = "C:\\myData\\myfGDB.gdb".public IWorkspace FileGdbWorkspaceFromPropertySet(string database)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("DATABASE", database);
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
[VB.NET]
' For example, database = "C:\myData\myfGDB.gdb".PublicFunction FileGdbWorkspaceFromPropertySet(ByVal database AsString) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("DATABASE", database)
Dim workspaceFactory As IWorkspaceFactory = New FileGDBWorkspaceFactoryClass()
Return workspaceFactory.Open(propertySet, 0)
EndFunction
The OpenFromFile method can also be used to open a file geodatabase workspace. As with the personal geodatabase workspace, OpenFromFile is the most commonly used method to open a file geodatabase workspace. See the following code example:
[C#]
// For example, path = "C:\\myData\\myfGDB.gdb".public IWorkspace FileGdbWorkspaceFromPath(String path)
{
IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactoryClass();
return workspaceFactory.OpenFromFile(path, 0);
}
[VB.NET]
' For example, path = "C:\myData\myfGDB.gdb".PublicFunction FileGdbWorkspaceFromPath(ByVal Path AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory = New FileGDBWorkspaceFactoryClass()
Return workspaceFactory.OpenFromFile(Path, 0)
EndFunction
File geodatabases can also be opened using the IWorkspaceFactory.OpenFromString method as shown in the following code example. This method is often more convenient than building the property set used with Open.
[C#]
// For example, connectionString = "DATABASE=C:\\myData\\myfGDB.gdb".public IWorkspace FileGdbWorkspaceFromString(string connectionString)
{
IWorkspaceFactory2 workspaceFactory = new FileGDBWorkspaceFactoryClass();
return workspaceFactory.OpenFromString(connectionString, 0);
}
[VB.NET]
' For example, connectionString = "DATABASE=C:\myData\myfGDB.gdb".PublicFunction FileGdbWorkspaceFromString(ByVal connectionString AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory2 = New FileGDBWorkspaceFactoryClass()
Return workspaceFactory.OpenFromString(connectionString, 0)
EndFunction
Connecting to an enterprise ArcSDE geodatabase workspace
SdeWorkspaceFactory is the required workspace factory to connect to an ArcSDE geodatabase. The connection properties of an esriRemoteDatabaseWorkspace specify the server and instance to connect to and can be saved in a connection file (.sde) on the file system.
In the case of remote database workspaces accessed via ArcSDE, the properties can include the following connection properties of the database being connected to:
SERVER—The SDE server being connected to.
INSTANCE—The instance being connected to.
DATABASE—The database being connected to. The DATABASE property is optional and is required for ArcSDE instances that manage multiple databases (for example, SQL Server).
USER—The connected user.
PASSWORD—The connected user's password.
AUTHENTICATION_MODE—The credential authentication mode of the connection. Acceptable values are the operating system authentication (OSA) and database management system (DBMS); the default is DBMS. If the AUTHENTICATION_MODE is OSA, the USER and PASSWORD properties are not required. OSA uses the operating system credentials to establish a connection with the database.
VERSION—The transactional version to connect to. The acceptable value is a string that represents a transaction version name.
HISTORICAL_NAME—The historical version to connect to. The acceptable value is a string that represents a historical marker name.
HISTORICAL_TIMESTAMP—The moment in history to establish a historical version connection. The acceptable value is a date object containing a date time that represents a moment time stamp. The formatting of the date is DBMS specific (for example, SQLServer = "1/1/2006 12:00:01 AM").
Since the workspace connection can only represent one version, only one of the three version properties (VERSION, HISTORICAL_NAME, or HISTORICAL_TIMESTAMP) should be used. If no version is supplied, a connection to the default transactional version will be returned.
If any of the Open methods for a workspace are called with insufficient properties, a connection dialog box opens to request the required properties. This can be used with ArcSDE workspaces to allow the user to connect with specific credentials (for example, enter a specific user name or password).
Connecting to a transactional version
In the following code example, a property set is populated from supplied strings to make a connection to an ArcSDE geodatabase workspace with a transactional version. The property set is then used by the Open method to return a workspace. This approach involves the most code; however, it can be customized into a general function for connecting to ArcSDE databases based on supplied parameters from the user.
[C#]
// For example, server = "Kona".// Database = "SDE" or "" if Oracle.// Instance = "5151".// User = "vtest".// Password = "go".// Version = "SDE.DEFAULT".public IWorkspace VersionedArcSdeWorkspace(string server, string instance,
string user, string password, string database, string version)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVER", server);
propertySet.SetProperty("INSTANCE", instance);
propertySet.SetProperty("DATABASE", database);
propertySet.SetProperty("USER", user);
propertySet.SetProperty("PASSWORD", password);
propertySet.SetProperty("VERSION", version);
IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
[VB.NET]
' For example, server = "Kona".' Database = "SDE" or "" if Oracle.' Instance = "5151".' User = "vtest".' Password = "go".' Version = "SDE.DEFAULT".PublicFunction VersionedArcSdeWorkspace(ByVal server AsString, ByVal instance AsString, ByVal user AsString, ByVal password AsString, ByVal database AsString, ByVal Version AsString) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("SERVER", server)
propertySet.SetProperty("INSTANCE", instance)
propertySet.SetProperty("DATABASE", database)
propertySet.SetProperty("USER", user)
propertySet.SetProperty("PASSWORD", password)
propertySet.SetProperty("VERSION", Version)
Dim workspaceFactory As IWorkspaceFactory = New SdeWorkspaceFactoryClass()
Return workspaceFactory.Open(propertySet, 0)
EndFunction
Connecting to a historical marker name
If archiving is enabled on data in an ArcSDE geodatabase, a historical version can be specified when connecting to the geodatabase. It is possible to create a named marker at some point. These markers are created with IHistoricalWorkspace and can be used to make a connection to this specific time.
To connect to a historical marker, augment the property set and replace the VERSION property with a HISTORICAL_NAME property. In the following code example, the name of the historical marker is Year End 2006:
[C#]
// For example, server = "Kona".// Database = "sde" or "" if Oracle.// Instance = "5151".// User = "vtest".// Password = "go".// Historical_name = "Year End 2006".public IWorkspace HistoricalArcSdeWorkspace(string server, string instance,
string user, string password, string database, string historical_name)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVER", server);
propertySet.SetProperty("INSTANCE", instance);
propertySet.SetProperty("DATABASE", database);
propertySet.SetProperty("USER", user);
propertySet.SetProperty("PASSWORD", password);
propertySet.SetProperty("HISTORICAL_NAME", historical_name);
IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
[VB.NET]
' For example, server = "Kona".' Database = "sde" or "" if Oracle.' Instance = "5151".' User = "vtest".' Password = "go".' Historical_name = "Year End 2006".PublicFunction HistoricalArcSdeWorkspace(ByVal server AsString, ByVal instance AsString, ByVal user AsString, ByVal password AsString, ByVal database AsString, ByVal historical_name AsString) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("SERVER", server)
propertySet.SetProperty("INSTANCE", instance)
propertySet.SetProperty("DATABASE", database)
propertySet.SetProperty("USER", user)
propertySet.SetProperty("PASSWORD", password)
propertySet.SetProperty("HISTORICAL_NAME", historical_name)
Dim workspaceFactory As IWorkspaceFactory = New SdeWorkspaceFactoryClass()
Return workspaceFactory.Open(propertySet, 0)
EndFunction
Connecting to a historical time stamp
It is also possible to connect at any time with the historical time stamp property. The following code example replaces the Historical_Name property with the Historical_Timestamp to connect to an ArcSDE workspace on SQL Server. It is good practice to convert a string representing a date to a date object when using it throughout the archiving API. This allows any date conversion errors to be handled before they are used to connect to a workspace.
[C#]
// For example, server = "Kona".// Database = "sde".// Instance = "5151".// User = "vtest".// Password = "go".// TimeStamp = "1/1/2006 12:00:01 AM".public IWorkspace TimestampedArcSdeWorkspace(string server, string instance,
string user, string password, string database, string timeStamp)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVER", server);
propertySet.SetProperty("INSTANCE", instance);
propertySet.SetProperty("DATABASE", database);
propertySet.SetProperty("USER", user);
propertySet.SetProperty("PASSWORD", password);
// It is a good practice to convert a string representing a date to a date object when using it // throughout the archiving API.
DateTime dateTimeStamp = Convert.ToDateTime(timeStamp);
propertySet.SetProperty("HISTORICAL_TIMESTAMP", dateTimeStamp);
IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
[VB.NET]
' For example, server = "Kona".' Database = "sde".' Instance = "5151".' User = "vtest".' Password = "go".' TimeStamp = "1/1/2006 12:00:01 AM".PublicFunction TimestampedArcSdeWorkspace(ByVal server AsString, ByVal instance AsString, ByVal user AsString, ByVal password AsString, ByVal database AsString, ByVal timeStamp AsString) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("SERVER", server)
propertySet.SetProperty("INSTANCE", instance)
propertySet.SetProperty("DATABASE", database)
propertySet.SetProperty("USER", user)
propertySet.SetProperty("PASSWORD", password)
' It is a good practice to convert a string representing a date to a date object when using it' throughout the archiving API.Dim dateTimeStamp As DateTime = Convert.ToDateTime(timeStamp)
propertySet.SetProperty("HISTORICAL_TIMESTAMP", dateTimeStamp)
Dim workspaceFactory As IWorkspaceFactory = New SdeWorkspaceFactoryClass()
Return workspaceFactory.Open(propertySet, 0)
EndFunction
If an ArcSDE connection file created in ArcCatalog (or created programmatically with IWorkspaceFactory.Create) is used to organize and distribute the connection information for an ArcSDE geodatabase, the OpenFromFile method can be used to connect to the workspace.
In the following code example, the path to the .sde connection file stored on disk is used to create the connection with the OpenFromFile method:
[C#]
// For example, connectionFile = "C:\\myData\\Connection to Kona.sde".public IWorkspace ArcSdeWorkspaceFromFile(String connectionFile)
{
IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
return workspaceFactory.OpenFromFile(connectionFile, 0);
}
[VB.NET]
' For example, connectionFile = "C:\myData\Connection to Kona.sde".PublicFunction ArcSdeWorkspaceFromFile(ByVal connectionFile AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory = New SdeWorkspaceFactoryClass()
Return workspaceFactory.OpenFromFile(connectionFile, 0)
EndFunction
The OpenFromString method is most commonly used for convenience. An example of this is when a specific application accesses an ArcSDE geodatabase with well known connection properties. In this case, creating a string of the name-value pairs may be easier then creating a property set for the same parameters.
In the following code example, connection is made to the same ArcSDE instance as in the Open method example, except as a different user:
[C#]
// For example, connectionString = "SERVER=Kona;DATABASE=sde;INSTANCE=5151;USER=Editor;PASSWORD=Editor;VERSION=sde.DEFAULT".public IWorkspace ArcSdeWorkspaceFromString(string connectionString)
{
IWorkspaceFactory2 workspaceFactory = new SdeWorkspaceFactoryClass();
return workspaceFactory.OpenFromString(connectionString, 0);
}
[VB.NET]
' For example, connectionString = "SERVER=Kona;DATABASE=sde;INSTANCE=5151;USER=Editor;PASSWORD=Editor;VERSION=sde.DEFAULT".PublicFunction ArcSdeWorkspaceFromString(ByVal connectionString AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory2 = New SdeWorkspaceFactoryClass()
Return workspaceFactory.OpenFromString(connectionString, 0)
EndFunction
Connecting to a personal or workgroup ArcSDE geodatabase workspace
Personal and workgroup ArcSDE geodatabases are remote database workspaces and use the SdeWorkspaceFactory to make a connection to the workspace.
Using the workspace factory
Personal and workgroup ArcSDE geodatabases can be connected to using the same code. The code for both is also very similar to that of an enterprise ArcSDE geodatabase. Personal and workgroup ArcSDE geodatabases only support connections using direct connect and do not support the three-tier connections that an enterprise ArcSDE does.
Additionally, connections can only be made using OSA, where the user's operating system credentials are used for the user name and password. The geodatabases in a personal and workgroup ArcSDE are always owned by the database owner (dbo) user; the version names are prefixed by dbo.
The following code example contains minor differences from the code used to connect to an enterprise geodatabase. The direct connection is set up in the instance property. In this code example, the AUTHENTICATION_MODE property is also introduced. This property defaults to DBMS, which means that the DBMS will be used to authenticate the user's credentials. The authentication mode has been set to OSA instead of passing in the user name and password.
[C#]
// For example, for direct connect with OSA authentication.// Server = "tivo_sqlexpress".// Instance = "sde:sqlserver:tivo\\sqlexpress"// Database = "sewer".// Authentication_mode = "OSA".// Version = "dbo.DEFAULT".public IWorkspace WorkgroupArcSdeWorkspaceFromPropertySet(string server, string
instance, string authentication_mode, string database, string version)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVER", server);
propertySet.SetProperty("INSTANCE", instance);
propertySet.SetProperty("DATABASE", database);
propertySet.SetProperty("AUTHENTICATION_MODE", authentication_mode);
propertySet.SetProperty("VERSION", version);
IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
[VB.NET]
' For example, for direct connect with OSA authentication.' Server = "tivo_sqlexpress".' Instance = "sde:sqlserver:tivo\sqlexpress"' Database = "sewer".' Authentication_mode = "OSA".' Version = "dbo.DEFAULT".PublicFunction WorkgroupArcSdeWorkspaceFromPropertySet(ByVal server AsString, ByVal instance AsString, ByVal authentication_mode AsString, ByVal database AsString, ByVal Version AsString) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("SERVER", server)
propertySet.SetProperty("INSTANCE", instance)
propertySet.SetProperty("DATABASE", database)
propertySet.SetProperty("AUTHENTICATION_MODE", authentication_mode)
propertySet.SetProperty("VERSION", Version)
Dim workspaceFactory As IWorkspaceFactory = New SdeWorkspaceFactoryClass()
Return workspaceFactory.Open(propertySet, 0)
EndFunction
The OpenFromFile method can also be used to connect to a personal or workgroup ArcSDE geodatabase. These files can be created manually in ArcCatalog or by using the Save Connection command after right-clicking a database server. This will create a .sde connection file in the Database Connections folder.
The connection files are then used in the OpenFromFile method as shown in the following code example:
[C#]
// For example, connectionFile = "C:\\myData\\Connection to tivo.sde".public IWorkspace WorkgroupArcSdeWorkspaceFromFile(String connectionFile)
{
IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
return workspaceFactory.OpenFromFile(connectionFile, 0);
}
[VB.NET]
' For example, connectionFile = "C:\myData\Connection to tivo.sde".PublicFunction WorkgroupArcSdeWorkspaceFromFile(ByVal connectionFile AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory = New SdeWorkspaceFactoryClass()
Return workspaceFactory.OpenFromFile(connectionFile, 0)
EndFunction
Using the OpenFromString method to open a personal or workspace ArcSDE geodatabase only differs from opening an enterprise ArcSDE geodatabase because of the string that is passed in. Ensure that OSA is set and that direct connect is used for the instance property value pair. See the following code example:
[C#]
// For example, connectionString = "SERVER=tivo_sqlexpress;DATABASE=sewer;INSTANCE=sde:sqlserver:tivo\\sqlexpress;AUTHENTICATION_MODE=OSA;VERSION=dbo.DEFAULT".public IWorkspace WorkgroupArcSdeWorkspaceFromString(string connectionString)
{
IWorkspaceFactory2 workspaceFactory = new SdeWorkspaceFactoryClass();
return workspaceFactory.OpenFromString(connectionString, 0);
}
[VB.NET]
' For example, connectionString = "SERVER=tivo_sqlexpress;DATABASE=sewer;INSTANCE=sde:sqlserver:tivo\sqlexpress;AUTHENTICATION_MODE=OSA;VERSION=dbo.DEFAULT".PublicFunction WorkgroupArcSdeWorkspaceFromString(ByVal connectionString AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory2 = New SdeWorkspaceFactoryClass()
Return workspaceFactory.OpenFromString(connectionString, 0)
EndFunction
Using the DataServerManager approach for a personal or workgroup ArcSDE
The DataServerManager object is used to access and administer one or more geodatabases stored on a data server. A connection can be opened to each geodatabase in a data server using the IWorkspaceName and IName interfaces. Unlike an enterprise ArcSDE geodatabase, opening a connection to a geodatabase stored in a personal or workgroup ArcSDE does not require a set of properties. Instead, the geodatabase name, type of version, and version specifier are used.
There are only three types of versions that can be used to connect to the geodatabase. See the following:
VERSION—Transactional version to connect to. The acceptable value is a string that represents a transaction version name.
HISTORICAL_NAME—Historical version to connect to. The acceptable value is a string that represents a historical marker name.
HISTORICAL_TIMESTAMP—Moment in history to establish a historical version connection. The acceptable value is a date object containing a date time that represents a moment time stamp.
The version specifier must correspond to the type of version (for example, if VERSION is specified, the version name must be listed as the version specifier). Also, since the workspace connection can only represent one version, only one of the three version properties (VERSION, or HISTORICAL_NAME, or HISTORICAL_TIMESTAMP) should be used.
The following code example shows how to connect to a transactional version of a geodatabase stored in a DataServer for a personal or workgroup ArcSDE using the DataServerManager:
[C#]
// For example, serverName = "tivo\\sqlexpress"// gdbName = "sewer"// versionPropName = "VERSION"// versionName = "dbo.DEFAULT"public IWorkspace WorkgroupArcSdeWorkspaceFromDSM(string serverName, string
gdbName, string versionPropName, string versionName)
{
// Create a Data Server Manager object.
IDataServerManager dataServerManager = new DataServerManagerClass();
// Set the server name and connect to the server.
dataServerManager.ServerName = serverName;
dataServerManager.Connect();
// Open one of the geodatabases in the Database Server.
IDataServerManagerAdmin dataServerManagerAdmin = (IDataServerManagerAdmin)
dataServerManager;
IWorkspaceName workspaceName = dataServerManagerAdmin.CreateWorkspaceName
(gdbName, versionPropName, versionName);
// Cast from the workspace name to utilize the Open method.
IName name = (IName)workspaceName;
IWorkspace workspace = (IWorkspace)name.Open();
return workspace;
}
[VB.NET]
' For example, serverName = "tivo\sqlexpress"' gdbName = "sewer"' versionPropName = "VERSION"' versionName = "dbo.DEFAULT"PublicFunction WorkgroupArcSdeWorkspaceFromDSM(ByVal serverName AsString, ByVal gdbName AsString, ByVal versionPropName AsString, ByVal versionName AsString) As IWorkspace
' Create a Data Server Manager object.Dim dataServerManager As IDataServerManager = New DataServerManagerClass()
' Set the server name and connect to the server.
dataServerManager.ServerName = serverName
dataServerManager.Connect()
' Open one of the geodatabases in the Database Server.Dim dataServerManagerAdmin As IDataServerManagerAdmin = CType(dataServerManager, IDataServerManagerAdmin)
Dim workspaceName As IWorkspaceName = dataServerManagerAdmin.CreateWorkspaceName(gdbName, versionPropName, versionName)
' Cast from the workspace name to utilize the Open method.Dim Name As IName = CType(workspaceName, IName)
Dim workspace As IWorkspace = CType(Name.Open(), IWorkspace)
Return workspace
EndFunction
Connecting to a shapefile workspace using the geodatabase API
The geodatabase is not only a physical store for geographic data, it is also the API used to access other types of geographic data with ArcGIS. Therefore, the geodatabase API can be used to access non-geodatabase workspaces such as shapefiles. Shapefiles have a workspace type of esriFileSystemWorkspace.
Like the geodatabase workspace, a workspace factory must be instantiated to access a shapefile workspace. Unlike geodatabases, the workspace factory for shapefiles is found in the DataSourcesFile library.
In the following code example, the Open method is used with a shapefile workspace factory to return the shapefile workspace. The workspace for shapefiles is the folder in which they are stored.
[C#]
// For example, path = "C:\\myData".public IWorkspace ShapefileWorkspaceFromPropertySet(string path)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("DATABASE", path);
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
[VB.NET]
' For example, path = "C:\myData".PublicFunction ShapefileWorkspaceFromPropertySet(ByVal Path AsString) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("DATABASE", Path)
Dim workspaceFactory As IWorkspaceFactory = New ShapefileWorkspaceFactoryClass()
Return workspaceFactory.Open(propertySet, 0)
EndFunction
The OpenFromFile method can also be used to open a shapefile geodatabase workspace. This method requires only the path of the folder containing the shapefile and is often the simplest solution for connecting to a shapefile workspace. See the following code example:
[C#]
// For example, path = "C:\\myData\\".public IWorkspace ShapefileWorkspaceFromPath(string path)
{
IWorkspaceFactory workspaceFactory = new ShapefileWorkspaceFactoryClass();
return workspaceFactory.OpenFromFile(path, 0);
}
[VB.NET]
' For example, path = "C:\myData\".PublicFunction ShapefileWorkspaceFromPath(ByVal Path AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory = New ShapefileWorkspaceFactoryClass()
Return workspaceFactory.OpenFromFile(Path, 0)
EndFunction
As with geodatabase workspaces, shapefile workspaces also support the IWorkspaceFactory2 interface and the OpenFromString method. As shown in the following code example, the input string is slightly larger than that required for the OpenFromFile method and might reduce the convenience normally associated with this method.
[C#]
// For example, connectionString = "DATABASE=C:\\myData".public IWorkspace ShapefileWorkspaceFromString(string connectionString)
{
IWorkspaceFactory2 workspaceFactory = new ShapefileWorkspaceFactoryClass();
return workspaceFactory.OpenFromString(connectionString, 0);
}
[VB.NET]
' For example, connectionString = "DATABASE=C:\myData".PublicFunction ShapefileWorkspaceFromString(ByVal connectionString AsString) As IWorkspace
Dim workspaceFactory As IWorkspaceFactory2 = New ShapefileWorkspaceFactoryClass()
Return workspaceFactory.OpenFromString(connectionString, 0)
EndFunction
Connecting to a coverage workspace using the geodatabase API
Along with shapefiles, coverages also have a workspace type of esriFileSystemWorkspace and can be accessed (with limited functionality) through the geodatabase API. The workspace for coverages is not a folder containing .pat and .adf files, but rather the parent directory with access to both that directory as well as the coverage's info directory. Although all three methods for opening shapefile workspaces will work with coverages, only one code example is shown, as the process of opening coverage workspaces is the same as opening shapefile workspaces, with the exception of the type of factory used.
In the following code example, the Open method is used with a shapefile workspace factory to return the shapefile workspace:
[C#]
// For example, path = "C:\\myData".public IWorkspace CoverageWorkspaceFromPropertySet(string path)
{
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("DATABASE", path);
IWorkspaceFactory workspaceFactory = new ArcInfoWorkspaceFactoryClass();
return workspaceFactory.Open(propertySet, 0);
}
[VB.NET]
' For example, path = "C:\myData".PublicFunction CoverageWorkspaceFromPropertySet(ByVal Path AsString) As IWorkspace
Dim propertySet As IPropertySet = New PropertySetClass()
propertySet.SetProperty("DATABASE", Path)
Dim workspaceFactory As IWorkspaceFactory = New ArcInfoWorkspaceFactoryClass()
Return workspaceFactory.Open(propertySet, 0)
EndFunction