This document is archived and information here might be outdated.  Recommended version.


Working with ArcSDE data (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Using geoprocessing > Accessing a dataset > Working with ArcSDE data (ArcObjects .NET 10.6 SDK)

Working with ArcSDE data


In this topic


ArcSDE data connections

Working with ArcSDE data requires a connection to an ArcSDE database. In geoprocessing, you can use either a stored ArcSDE connection file or a property set to make the connection.

Creating a connection file

The two ways to create a connection file are as follows:
  • Use the Spatial Database Connection wizard from the Database Connections node in the Catalog tree. 
  • Use the Create ArcSDE Connection File tool (in the Data Management toolbox).
In both cases, a file with a .sde extension is created.

Using the Create ArcSDE Connection File tool

You can use the Create ArcSDE Connection File tool in the Data Management toolbox to create a .sde file on disk. The following code example shows how to create the file:
[C#]
public void createSDEConnectionFile(IGeoProcessor2 gp)
{
    IVariantArray parameters=new VarArrayClass();
    parameters.Add(@"C:\temp");
    parameters.Add("flames.sde");
    parameters.Add("wilma4");
    parameters.Add("5940");
    parameters.Add("vtest");
    parameters.Add("true");
    parameters.Add("toolbox");
    parameters.Add("toolbox");
    parameters.Add("true");
    parameters.Add("sde.DEFAULT");

    IGeoProcessorResult result=null;

    try
    {
        result=(IGeoProcessorResult)gp.Execute(
            "CreateArcSDEConnectionFile_management", parameters, null);
        string conn_file=result.GetOutput(0).GetAsText();
        // Use the connection file for further geoprocessing.
        // ...
    }
    catch (Exception ex)
    {
        // Print exception and error messages.
    }
}
[VB.NET]
Public Sub createSDEConnectionFile(IGeoProcessor2 gp)
    
    Dim parameters As IVariantArray=New VarArray
    parameters.Add("C:\temp")
    parameters.Add("flames.sde")
    parameters.Add("wilma4")
    parameters.Add("5940")
    parameters.Add("vtest")
    parameters.Add("true")
    parameters.Add("toolbox")
    parameters.Add("toolbox")
    parameters.Add("true")
    parameters.Add("sde.DEFAULT")
    
    Dim result As IGeoProcessorResult=Nothing
    
    Try
    
    result=CType(gp.Execute("CreateArcSDEConnectionFile_management", parameters, null), IGeoProcessorResult)
    Dim conn_file As String=result.GetOutput(0).GetAsText()
    ' Use the connection file for further geoprocessing.
    ' ...
    Catch(ex As Exception)
    ' Print exception and error messages.
    End Try
    
End Sub

Using a connection file

Once you have created a connection file, the catalog path to the .sde file is used as the workspace in geoprocessing. See the following code example:
[C#]
public void SDEConnectionFile(IGeoProcessor2 gp)
{
    gp.SetEnvironmentValue("workspace", @"C:\St_Johns");
    IGpEnumList fcs=gp.ListFeatureClasses("*", "", "");

    // Set the workspace to SDE for ValidateTableName.
    gp.ResetEnvironments();
    string wks=@"Database Connections\Bluestar.sde";
    gp.SetEnvironmentValue("workspace", wks);

    string fc=fcs.Next();
    string outfc="";
    IVariantArray parameters=new VarArrayClass();

    while (fc != "")
    {
        // Validate the output name.
        outfc=gp.ValidateTableName(fc, wks);

        // Copy the features from D:/St_Johns to the SDE workspace.
        parameters.Add(fc);
        parameters.Add(outfc);
        gp.Execute("CopyFeatures_management", parameters, null);
        fc=fcs.Next();
    }
}
[VB.NET]
Public Sub SDEConnectionFile(ByVal gp As IGeoProcessor2)
    
    gp.SetEnvironmentValue("workspace", "D:\St_Johns")
    Dim fcs As IGpEnumList=gp.ListFeatureClasses("*", "", "")
    
    ' Set the workspace to SDE for ValidateTableName.
    gp.ResetEnvironments()
    Dim wks As String="Database Connections\Bluestar.sde"
    gp.SetEnvironmentValue("workspace", wks)
    
    Dim fc As String=fcs.Next
    Dim outfc As String=""
    Dim parameters As IVariantArray=New VarArray
    
    While Not fc Is ""
        
        ' Validate the output name.
        outfc=gp.ValidateTableName(fc, wks)
        
        ' Copy the feature classes from D:/St_Johns to the SDE workspace.
        parameters.Add(fc)
        parameters.Add(outfc)
        gp.Execute("CopyFeatures_management", parameters, Nothing)
        
        fc=fcs.Next
        
    End While
    
End Sub
Whether you use the Desktop Wizard or the geoprocessing tool to create the connection file, you have to use the stored *.sde file.


See Also:

Geoprocessing considerations for ArcSDE data
Connecting to geodatabases and databases




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):