ArcGIS 10.1 – Declaring ArcSDE Workspace for Geoprocessing Tool Using ArcObjects and C#

arcgis-10.1arcobjectscgeoprocessing

What is the proper way to declare an ArcSDE workspace for Geoprocessing tools in c# .net?

I am using ArcGIS 10.1 SDE (MS SQL) on SQL Server 2008. I want to write code so that every night the server will fire a program and compress one of my ArcSDE databases. I thought I could do this in c# using the Compress tool in the data management namespace but have run into a snag.

I am told my workspace is invalid. Esri help docs are not helpful, as they just show generic help and my internet search has lead nowhere.

Can anyone tell me how to define an ArcSDE workspace in 10.1 in net?

Right now I am using @"Database Connections\CME.sde". Here is the error:

Executing: Compress "Database Connections\CME.sde"
Start Time: Fri Oct 12 13:40:47 2012
Failed to execute. Parameters are not valid.
ERROR 000837: The workspace is not the correct workspace type.
Failed to execute (Compress).

What should I type for the in_workspace?


private void button1_Click(object sender, EventArgs e)
        {

            Compress Compressor = new Compress();
            object in_workspace = @"Database Connections\CME.sde";




            Compressor.in_workspace = in_workspace;

            Geoprocessor GP = new Geoprocessor();
            RunTool(GP, Compressor, null);


        }
        private static void RunTool(Geoprocessor geoprocessor, IGPProcess process, ITrackCancel TC)
        {

            // Set the overwrite output option to true
            geoprocessor.OverwriteOutput = true;

            // Execute the tool            
            try
            {
                geoprocessor.Execute(process, null);
                ReturnMessages(geoprocessor);

            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message); 
                ReturnMessages(geoprocessor);
            }
        }
        private static void ReturnMessages(Geoprocessor gp)
        {
            if (gp.MessageCount > 0)
            {
                for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
                {
                  Console.WriteLine(gp.GetMessage(Count));

                }
            }

        }
    }

Best Answer

this code returns an SDE workspace, hopes can help

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

    }
Related Question