Connect to geodatabase using .SDE file

3738
3
04-25-2014 08:53 AM
JamariPowers
New Contributor III
Working in C#

I have been working with arcObjects and researching connecting to a geodatabase. I already have a working .SDE file created. Again, my lack of understanding and the convolution from the ArcGIS resource center is causing me confusion. I am not sure where to start exactly. I have bits and pieces as far as understanding.

I believe I must start with an IFeatureWorkspace. From this I am supposed to use sdeWorkspaceFactoryClass, correct? I was going to use a line of code as follows:

IWorkspaceFactory workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
m_ReadOnlyWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(nameOfFile, 0);

where "m_ReadOnlyWorkspace" is my created workspace and "nameOfFile" is my sde file. Should my "nameOfFile" variable be the entire path to the .SDE file or just the name of the file?

Once I am able to make this line work, I should be [connected] to the database and able to run a query from a table correct? Am I missing any intermediate steps in the process?

Any help or insight on these issues / questions would be greatly appreciated. Thanks in advance.
0 Kudos
3 Replies
AhmedEl-Sisi
Occasional Contributor III
it should be the entire path of the file

// For example, connectionFile = @"C:\myData\Connection to Kona.sde".
public static IWorkspace ArcSdeWorkspaceFromFile(String connectionFile)
{
    Type factoryType = Type.GetTypeFromProgID(
        "esriDataSourcesGDB.SdeWorkspaceFactory");
    IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance
        (factoryType);
    return workspaceFactory.OpenFromFile(connectionFile, 0);
}

once you get your IFeatureWorkspace, you can get your featureclass/table
for more info:
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//0001000003s8000000
http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/IFeatureWorkspace_Interf...
0 Kudos
JamariPowers
New Contributor III
Thanks for your response. I think I may have got it. I created a function myself. The script is below:

public static class Utilities
    {
         public static IFeatureWorkspace m_FeatureWorkspace =null;
         private static IFeatureWorkspace m_ReadOnlyWorkspace = null;
         private static string _path = "C:\\Users\\Me\\MyStuff\\MyProject\\myFile.sde";
        

        
        public static IFeatureWorkspace connect()
        {
            IWorkspaceFactory _workspaceFactory = new ESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();
            m_ReadOnlyWorkspace = (IFeatureWorkspace)_workspaceFactory.OpenFromFile(_path, 1);

            return m_ReadOnlyWorkspace;
        }

    
    }


I believe this to be correct and working. What do you think?

I guess I would need to be sure, so I am also working on a conditional statement to "do something" or output a message whether I am actually connected to the sde file or not.
0 Kudos
AhmedEl-Sisi
Occasional Contributor III
Your code should work fine.
If you method returns an object you should be connected.
0 Kudos