HOW TO
In an ArcObjects or ArcGIS Engine client application, we may want to connect to a multiuser geodatabase workspace, such as Oracle or SQL Server, using the IPropertySet interface. A code sample showing this workflow is provided below:
C# string dbclient = "sqlserver"; string dbConnProp = @"myMachineName\SQLEXPRESS"; string database = "myDatabase"; string user = "myUserName"; string password = "myPassword"; string version = "sde.DEFAULT"; string authentication = "DBMS"; IPropertySet propertySet = new PropertySet(); propertySet.SetProperty("DBCLIENT", dbclient); propertySet.SetProperty("DB_CONNECTION_PROPERTIES", dbConnProp); propertySet.SetProperty("DATABASE", database); propertySet.SetProperty("USER", user); propertySet.SetProperty("PASSWORD", password); propertySet.SetProperty("VERSION", version); propertySet.SetProperty("AUTHENTICATION_MODE", authentication); Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); IWorkspace workspace = workspaceFactory.Open(propertySet, 0);
On running the above code, one might receive a runtime exception, such as the following:
Error: System.Runtime.InteropSerives.COMException: "Underlying DBMS error[ORA-12545: Connect failed because target host or object does not exist. No extended error.]
This exception is most likely caused by a typo in one of the above properties in the property set.
One way to resolve this problem is to work in reverse and use the IPropertySet.GetAllProperties method to get all the SDE connection properties directly from an SDE connection file.
This SDE connection file can be generated by connecting to the multiuser (SDE) geodatabase from a desktop client, such as ArcCatalog. The path of the connection file can be copied from the Database properties window of the SDE connection in ArcCatalog:
Right-click the SDE Connection in the Catalog tree, and click Properties > General tab:
Name: C:\Users\myUserName\AppData\Roaming\ESRI\Desktop10.8\ArcCatalog\mySDEConnectionFile.sde
The GetAllProperties method is especially useful when we are unsure of the syntax of the property set values. It is easy to make syntactical errors when writing these out, and your ArcObjects code will fail to execute even with the smallest typo, for example, a missing colon, backslash, period, or dollar sign. The code sample below shows how to call this method and output the values to your Visual Studio debug console:
C# // Get the Workspace from an existing SDE file (after a successful connection to an Enterprise Geodatabase) IWorkspace workspace = IWorkspaceFactory.openFromFile(pathToSDEFile, int); // Use IPropertySet.GetAllProperties() to return all the property values from a successful Enterprise workspace connection. object oNames = null; object oValues = null; IPropertySet propertySet = IWorkspace.ConnectionProperties; propertySet.GetAllProperties(oNames, oValues); // Cast as array string[] stringArray_oNames = (string[])oNames; object[] objectArray_oValues = (object[])oValues; // Print out the property set name : value pairs for (int i = 0; i <= stringArray_oNames.Length - 1; i++) { Debug.Write("Name of Property(" + i.ToString() + "): " + stringArray_oNames(i) + ", "); Debug.WriteLine("Value(" + i.ToString() + "): " + objectArray_oValues(i).ToString()); }
The output of the above code would be name/value pairs similar to the sample output below:
Name of Property(0): SERVER, Value(0): myMachineName Name of Property(1): INSTANCE, Value(1): sde:sqlserver:myMachineName\SQLEXPRESS Name of Property(2): DBCLIENT, Value(2): sqlserver Name of Property(3): DB_CONNECTION_PROPERTIES, Value(3): myMachineName\SQLEXPRESS Name of Property(4): DATABASE, Value(4): myDatabase Name of Property(5): IS_GEODATABASE, Value(5): true Name of Property(6): AUTHENTICATION_MODE, Value(6): DBMS Name of Property(7): USER, Value(7): myUserName Name of Property(8): PASSWORD, Value(8): System.Byte[] Name of Property(9): CONNPROP-REV, Value(9): Rev1.0 Name of Property(10): VERSION, Value(10): sde.DEFAULT
You can now use the above property set values to connect to your SDE workspace using the code below:
IWorkspace workspace = workspaceFactory.Open(propertySet, 0);
Note about Licensing: If using ArcGIS Engine, make sure you have licensed your Engine app appropriately before making any ArcObjects API calls, or else an error will occur on the first ArcObjects API call. If using an ArcMap Add-in or custom COM component, make sure to license ArcMap appropriately.
Article ID: 000031500
Get help from ArcGIS experts
Download the Esri Support App