Select to view content in your preferred language

ArcSDE WorkspaceFactory Connection Error

878
1
Jump to solution
09-25-2012 07:53 AM
KenDoman
Frequent Contributor
Part of my addin is trying to connect to what could be a SDE connection. I'm using WorkspaceFactory.OpenfromFile to connect to the geodatabase, because the connection could be to a file geodatabase, personal geodatabase, or an sde connection.

Here's where my error is coming in. When I set the location of my connection file to the absolute location (C:\Users\ ... \ArcCatalog\myGIS.sde), it connects fine. But when I set it to "Database Connections\myGIS.sde", which is the location I get when I use the ArcCatalog dialog to browse to the geodatabase, the connection throws an error.

Is there a way to get the file location for the database connection file programmatically? I want to use OpenFromFile because I don't want to handle user names and passwords, and because I can open other geodatabases with the same code.

Here's an edited snippet of my code:

 ' code works when connection = "C:\Users\ ... \ArcCatalog\myGIS.sde"  Dim connection as String = "Database Connections\myGIS.sde"  Dim myWorkspace as IWorkspace Dim factoryType as Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory") Dim myWorkspaceFactory = Ctype(Activator.CreateInstance(factoryType), IWorkspaceFactory)  myWorkspace = myWorkspaceFactory.OpenFromFile(connection, 0) ' here is where I get the error. 
0 Kudos
1 Solution

Accepted Solutions
KenDoman
Frequent Contributor
Okay, after digging around, I finally worked it out. It turns out, database connections are stored in specific locations, based on the user profile. I just added code that replaces the Database Connections prefix with the expected location of the database connection.

 Dim connection as String = "Database Connections\myGIS.sde" ' ... other stuff  Dim factoryType as Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory") Dim myWorkspaceFactory = Ctype(Activator.CreateInstance(factoryType), IWorkspaceFactory)  If connection.StartsWith("Database Connections\") Then  Dim userProfile as String = System.Environment.GetEnvironmentVariable("UserProfile")  ' for 10.0 ' Dim fileLocation As String = userProfile & "\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\" ' for 10.1 Dim fileLocation As String = userProfile & "\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\"  connection = connection.Replace("Database Connections\", fileLocation)  End If  Dim myWorkspace as IWorkspace = myWorkspaceFactory.OpenFromFile(connection, 0) ' no more error 

View solution in original post

0 Kudos
1 Reply
KenDoman
Frequent Contributor
Okay, after digging around, I finally worked it out. It turns out, database connections are stored in specific locations, based on the user profile. I just added code that replaces the Database Connections prefix with the expected location of the database connection.

 Dim connection as String = "Database Connections\myGIS.sde" ' ... other stuff  Dim factoryType as Type = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory") Dim myWorkspaceFactory = Ctype(Activator.CreateInstance(factoryType), IWorkspaceFactory)  If connection.StartsWith("Database Connections\") Then  Dim userProfile as String = System.Environment.GetEnvironmentVariable("UserProfile")  ' for 10.0 ' Dim fileLocation As String = userProfile & "\AppData\Roaming\ESRI\Desktop10.0\ArcCatalog\" ' for 10.1 Dim fileLocation As String = userProfile & "\AppData\Roaming\ESRI\Desktop10.1\ArcCatalog\"  connection = connection.Replace("Database Connections\", fileLocation)  End If  Dim myWorkspace as IWorkspace = myWorkspaceFactory.OpenFromFile(connection, 0) ' no more error 
0 Kudos