How to use the ESRI OLE DB provider with a geodatabase
Development licensing
Deployment licensing
ArcView
ArcView
ArcEditor
ArcEditor
ArcInfo
ArcInfo
Engine Developer Kit
Engine Runtime
To use the code in this article, the following namespace must be referenced via the using (C#) or Imports (VB.NET) statements. It is also necessary to add the corresponding reference to the project in order to gain access to the API.
In some cases, it may be useful to connect to a geodatabase directly with customized applications. ArcGIS is equipped with a minimum (level 0) OLE DB provider to facilitate this type of approach.
The ESRI OLE DB provider currently supports the ActiveX® Data Objects (ADO) by Microsoft. For more information on the ADO object and programming model, see the ADO Programmer's Guide on the MSDN website.
The ESRI OLE DB provider only works with simple feature classes (i.e. containing points, polylines, polygons) and tables.
The following examples show how to use the ESRI OLE DB provider in a .NET environment and establish a connection to one of the five supported ESRI workspace factories:
For an ArcSDE database either use: Provider=ESRI.GeoDB.OLEDB.1;Location=server name;Data Source=database name; User Id=user name;Password=password; Extended Properties=WorkspaceType= esriDataSourcesGDB.SDEWorkspaceFactory.1;Geometry=WKB|OBJECT;Instance=ArcSDE service;Version=ArcSDE version
Or use: Provider=ESRI.GeoDB.OLEDB.1;Extended Properties=WorkspaceType= esriDataSourcesGDB.SDEWorkspaceFactory.1;ConnectionFile=path to and name of binary ArcSDE connection file
[C#]
// For example, server = "balrog"// userId = "vtest"// password = "go"// geometry = "WKB"// instance = "5150"// version = "SDE.DEFAULT"public OleDbConnection ArcSdeConnectionWithString(String server, String userId,
String password, String geometry, String instance, String version)
{
// Build the connection string.
String blankSdeConnectionString =
"Provider=ESRI.GeoDB.OleDB.1;Location={0};Data Source=sde;User Id={1};Password={2};" + "Extended Properties=workspacetype=esriDataSourcesGDB.SdeWorkspaceFactory.1;Geometry={3};Instance={4};Version={5}";
String connectionString = String.Format(blankSdeConnectionString, server,
userId, password, geometry, instance, version);
// Create the connection and return it.
OleDbConnection sdeConn = new OleDbConnection();
sdeConn.ConnectionString = connectionString;
return sdeConn;
}
[VB.NET]
' For example, server = "balrog"' userId = "vtest"' password = "go"' geometry = "WKB"' instance = "5150"' version = "SDE.DEFAULT"PublicFunction ArcSdeConnectionWithString(ByVal server AsString, ByVal userId AsString, ByVal password AsString, ByVal geometry AsString, ByVal instance AsString, ByVal Version AsString) As OleDbConnection
' Build the connection string.Dim blankSdeConnectionString AsString = "Provider=ESRI.GeoDB.OleDB.1;Location={0};Data Source=sde;User Id={1};Password={2};" & "Extended Properties=workspacetype=esriDataSourcesGDB.SdeWorkspaceFactory.1;Geometry={3};Instance={4};Version={5}"Dim connectionString AsString = String.Format(blankSdeConnectionString, server, userId, password, geometry, instance, Version)
' Create the connection and return it.Dim sdeConn As OleDbConnection = New OleDbConnection()
sdeConn.ConnectionString = connectionString
Return sdeConn
EndFunction
[C#]
// For example, connectionFile = "C:\\Temp\\MySdeConnection.sde"public OleDbConnection ArcSdeConnectionWithFile(String connectionFile)
{
// Build the connection string.
String sdeStringTemplate =
"Provider=ESRI.GeoDB.OleDB.1;Extended Properties=workspacetype=" +
"esriDataSourcesGDB.SdeWorkspaceFactory.1;ConnectionFile={0}";
String connectionString = String.Format(sdeStringTemplate, connectionFile);
// Create the connection and return it.
OleDbConnection sdeConn = new OleDbConnection();
sdeConn.ConnectionString = connectionString;
return sdeConn;
}
[VB.NET]
' For example, connectionFile = "C:\Temp\MySdeConnection.sde"PublicFunction ArcSdeConnectionWithFile(ByVal connectionFile AsString) As OleDbConnection
' Build the connection string.Dim sdeStringTemplate AsString = "Provider=ESRI.GeoDB.OleDB.1;Extended Properties=workspacetype=" & "esriDataSourcesGDB.SdeWorkspaceFactory.1;ConnectionFile={0}"Dim connectionString AsString = String.Format(sdeStringTemplate, connectionFile)
' Create the connection and return it.Dim sdeConn As OleDbConnection = New OleDbConnection()
sdeConn.ConnectionString = connectionString
Return sdeConn
EndFunction
Connecting to other ESRI data sources
The version parameter is optional in the connection string—a list of available ArcSDE versions will be returned if it is not included.
For connections to an ArcSDE service running on an Oracle platform, this parameter is case sensitive. If the version parameter is entered incorrectly, the request to open the connection fails.
A successful connection to the ArcSDE service returns all the available databases (for SQL Server platforms) and database objects. Access to these objects is controlled by database management system (DBMS) level user permissions.
The data source parameter is only used to connect to ArcSDE on a SQL Server platform with multiple databases. If the data source parameter is included, database objects can be referenced by the schema object name; for example, "select * from us_states." If the data source parameter is omitted, the fully qualified name of the schema object must be supplied—database name.schema name.object name; for example, "select * from world.world.us_states."
For ArcSDE connection to an Oracle database, again all available schema objects will be returned. Assuming the connected user has the appropriate permissions and schema object synonyms, it is not necessary to fully qualify the database object name. Without the correct schema object synonyms, the object must be identified using schema name.object name. The user name and password required for an ArcSDE connection are the relational database management system (RDBMS) user name and password.
Personal geodatabase (Access .mdb file)
The Access workspace factory requires the path to and the name of a Microsoft Access® file (.mdb). See the following:
// For example, path = @"C:\arcgis\ArcTutor\ArcReader and Publisher\Data"// geometry = "WKB"public OleDbConnection ShapefileConnection(String path, String geometry)
{
// Build the connection string.
String shapefileStringTemplate =
"Provider=ESRI.GeoDB.OleDB.1;Data Source={0};" +
"Extended Properties=workspacetype=esriDataSourcesFile.ShapefileWorkspaceFactory.1;Geometry={1}";
String connectionString = String.Format(shapefileStringTemplate, path,
geometry);
// Create the connection and return it.
OleDbConnection shapefileConn = new OleDbConnection();
shapefileConn.ConnectionString = connectionString;
return shapefileConn;
}
[VB.NET]
' For example, path = "C:\arcgis\ArcTutor\ArcReader and Publisher\Data"' geometry = "WKB"PublicFunction ShapefileConnection(ByVal Path AsString, ByVal geometry AsString) As OleDbConnection
' Build the connection string.Dim shapefileStringTemplate AsString = "Provider=ESRI.GeoDB.OleDB.1;Data Source={0};" & "Extended Properties=workspacetype=esriDataSourcesFile.ShapefileWorkspaceFactory.1;Geometry={1}"Dim connectionString AsString = String.Format(shapefileStringTemplate, Path, geometry)
' Create the connection and return it.Dim shapefileConn As OleDbConnection = New OleDbConnection()
shapefileConn.ConnectionString = connectionString
Return shapefileConn
EndFunction
Coverages
The coverage workspace factory also requires a path to a coverage directory, not a specific coverage. See the following:
Provider=ESRI.GeoDB.Oledb.1;Data Source=path to A/I coverage directory; Extended Properties=workspacetype=esriDataSourcesFile.ArcInfoWorkspaceFactory.1;Geometry=WKB|OBJECT
[C#]
// For example, path = @"C:\arcgis\ArcTutor\Catalog\Yellowstone"// geometry = "WKB"public OleDbConnection CoverageConnection(String path, String geometry)
{
// Build the connection string.
String coverageStringTemplate =
"Provider=ESRI.GeoDB.OleDB.1;Data Source={0};" +
"Extended Properties=workspacetype=esriDataSourcesFile.ArcInfoWorkspaceFactory.1;Geometry={1}";
String connectionString = String.Format(coverageStringTemplate, path,
geometry);
// Create the connection and return it.
OleDbConnection coverageConn = new OleDbConnection();
coverageConn.ConnectionString = connectionString;
return coverageConn;
}
[VB.NET]
' For example, path = "C:\arcgis\ArcTutor\Catalog\Yellowstone"' geometry = "WKB"PublicFunction CoverageConnection(ByVal Path AsString, ByVal geometry AsString) As OleDbConnection
' Build the connection string.Dim coverageStringTemplate AsString = "Provider=ESRI.GeoDB.OleDB.1;Data Source={0};" & "Extended Properties=workspacetype=esriDataSourcesFile.ArcInfoWorkspaceFactory.1;Geometry={1}"Dim connectionString AsString = String.Format(coverageStringTemplate, Path, geometry)
' Create the connection and return it.Dim coverageConn As OleDbConnection = New OleDbConnection()
coverageConn.ConnectionString = connectionString
Return coverageConn
EndFunction
ADO programming considerations for the ESRI OLE DB provider
The following recordset object.open method parameters are supported:
Source—A valid command object variable name, a SQL statement, a table name, a stored procedure call, or the file name of a persisted (saved) recordset.
ActiveConnection—A valid connection object or a string that contains connection string parameters.
Unregistered tables in a geodatabase cannot be edited with the ESRI OLE DB provider. The table can either be registered with the geodatabase or a native OLE DB provider for the RDBMS data source can be used.
DBMS Insert/Update/Delete SQL operations applied directly against an ADO connection to a personal or ArcSDE geodatabase feature class or registered table are not supported and are not recommended unless there is a great deal of familiarity with the data structure at source (such as associated indexes to be maintained).
To edit registered feature classes or tables, all updates must be applied against the ADO recordset. However, such operations are supported against unregistered tables. The ADO command syntax for this operation is: connection obj.Execute(SQL statement).
Neither of the file-based data sources (shapefiles and coverages) supports Insert/Update/Delete commands applied against the ADO connection. All updates to these data sources should be undertaken via the ADO recordset.
For best results when applying a number of updates to a recordset, always use an edit transaction. This significantly improves the performance for updating. The edit transaction is opened on the connection object—connection object.begintrans.
To successfully disconnect a recordset from a connection, set the cursor location to asUseClient and set the lock type to adLockBatchOptimistic. Propagating any changes back to the data source when the recordset is reassigned to an active connection requires support for the recordset updatebatch method. Currently, the ESRI OLE DB provider does not support this functionality.
To graphically represent the tabular recordset, use an ADO/OLEDB recordset with an MSChart object in Visual Basic. For best results, set the recordset cursor location to adUseClient and set the MSChart object's data source property to be the ADO recordset.
Simple recordset display
The following code example connects to a personal geodatabase, issues a SQL select statement, and displays the results:
[C#]
// For example, path = @"C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\GulfOfStLawrence\data"// shapefile = "Can_Mjr_Cities"publicvoid DisplayNamesFromShapefile(String path, String shapefile)
{
// Build the connection string.
String shapefileStringTemplate =
"Provider=ESRI.GeoDB.OleDB.1;Data Source={0};" +
"Extended Properties=workspacetype=esriDataSourcesFile.ShapefileWorkspaceFactory.1;Geometry=WKB";
String connectionString = String.Format(shapefileStringTemplate, path);
// Create the connection.
OleDbConnection oleDbConnection = new OleDbConnection();
oleDbConnection.ConnectionString = connectionString;
// Create the command.
String sqlQuery = String.Format("SELECT NAME FROM {0}", shapefile);
OleDbCommand oleDbCommand = new OleDbCommand(sqlQuery, oleDbConnection);
// Open the connection and create a reader.
oleDbConnection.Open();
OleDbDataReader oleDbDataReader = oleDbCommand.ExecuteReader();
// Display the values from the NAME field.while (oleDbDataReader.Read())
{
Console.WriteLine(oleDbDataReader[0].ToString());
}
}
[VB.NET]
' For example, path = "C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\GulfOfStLawrence\data"' shapefile = "Can_Mjr_Cities"PublicSub DisplayNamesFromShapefile(ByVal Path AsString, ByVal shapefile AsString)
' Build the connection string.Dim shapefileStringTemplate AsString = "Provider=ESRI.GeoDB.OleDB.1;Data Source={0};" & "Extended Properties=workspacetype=esriDataSourcesFile.ShapefileWorkspaceFactory.1;Geometry=WKB"Dim connectionString AsString = String.Format(shapefileStringTemplate, Path)
' Create the connection.Dim oleDbConnection As OleDbConnection = New OleDbConnection()
oleDbConnection.ConnectionString = connectionString
' Create the command.Dim sqlQuery AsString = String.Format("SELECT NAME FROM {0}", shapefile)
Dim oleDbCommand As OleDbCommand = New OleDbCommand(sqlQuery, oleDbConnection)
' Open the connection and create a reader.
oleDbConnection.Open()
Dim oleDbDataReader As OleDbDataReader = oleDbCommand.ExecuteReader()
' Display the values from the NAME field.DoWhile oleDbDataReader.Read()
Console.WriteLine(oleDbDataReader(0).ToString())
LoopEndSub