Thursday, 11 January 2018

How to declare ArcSDE workspace for Geoprocessing tool in ArcGIS 10.1 using ArcObjects and c#?


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));

}

}

}
}

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);

}

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...