Tips and Tricks – Creating ArcSDE connection files on the fly using Python and ArcObjects

ArcGIS 9.3 does not provide a geoprocessing tool for creating ArcSDE
connection files (.sde files). Some of you have requested such a tool for security
reasons – typically because you do not want .sde connection files hanging
around on machines. Rather, you want to automatically create a connection file
in a temporary location, use it in your script or model, and then delete it
within the process you are running. Others need the ability to create .sde files
on the fly because the connection properties, such as what version to connect to,
are not known at the start of the process. Having to stop the process for someone
to manually create the .sde file with the correct properties really isn’t an
option anyone wants to entertain.

The attached .zip file (download .zip file) contains a java class (CreateSDEConnFile) and a
python script (CreateSDEConnFile.py) to create
ArcSDE connection files on the fly. Download and unzip the file. Be sure to
unzip the file into a folder that does not contain spaces. For example:

E:SDE Conn sample (incorrect – contains spaces)
E:SDEConnectionSample (correct – no spaces)

Java must be installed

You must have Java installed in order to use the files included in the .zip file.
Most likely, Java is installed on your machine — it gets installed by many websites.
You can check by examining your installed programs list. You can install java by
visiting java.com.

Setting JAVA_HOME

In order for CreateSDEConnFile.py file to execute, you must have the JAVA_HOME
system environment variable set to the location of the java install directory.

On Windows XP, right-click “My Computer” on your desktop, click the Advanced tab, and click the
“Environment Variables” button. If the JAVA_HOME environment variable doesn’t exist,
create it by clicking the “New” button.
(On my machine, JAVA_HOME = C:Program FilesJavajre1.6.0_07)

About CreateSDEConnFile.py Python script

CreateSDEConnFile.py is a sample script.
You will need to modify it to include details about your ArcSDE configuration and security needs.
There is no associated geoprocessing script tool for CreateSDEConnFile.py — you will have to
create your own script tool for use in ModelBuilder (See An overview of creating script tools in the web help for
more information about creating script tools.)

Arguments to the CreateSDEConnFile java class

String sConFileDir = args[0]; //Folder path to store the .sde file.
String sConFile = args[1]; //.sde file name. include the .sde extension.
pConnProps.setProperty(“SERVER”, args[2]); //ArcSDE Server name.
pConnProps.setProperty(“INSTANCE”, args[3]); //ArcSDE port number.
pConnProps.setProperty(“USER”, args[4]); //ArcSDE user name.
pConnProps.setProperty(“PASSWORD”, args[5]); //ArcSDE password.
pConnProps.setProperty(“VERSION”, args[6]); //Version to connect to.
pConnProps.setProperty(“DATABASE”, args[7]); //Database to connect to.

zip file

Download zip file

This entry was posted in Analysis & Geoprocessing and tagged , . Bookmark the permalink.

Leave a Reply

3 Comments

  1. crrose says:

    I was able to modify this to run and created all of the .sde connection files needed, however, we are using Workgroup SDE and “Operating System Authentication” and not a specified UserName/Password. So when I try and use the connection files, it says bad login. Is there any way to modify this to use OSA instead? (I do not know Java)
    Thanks!

  2. crrose says:

    I was able to modify the arguments to:
    String sConFileDir = args[0]; //Folder path to store the .sde file.
    String sConFile = args[1]; //.sde file name. include the .sde extension.
    pConnProps.setProperty(“SERVER”, args[2]); //ArcSDE Server name.
    pConnProps.setProperty(“INSTANCE”, args[3]); //ArcSDE port number or Service.
    pConnProps.setProperty(“AUTHENTICATION_MODE”, args[4]); //Use “OSA”
    pConnProps.setProperty(“VERSION”, args[5]); //Version to connect to.
    pConnProps.setProperty(“DATABASE”, args[6]); //Database to connect to.

    Through my friend Google, I was able to finally figure out what I needed to do and how to compile the .java file I changed as above.

  3. crrose says:

    Would there be a limit to the path length of the variable javaClassLocation in the CreateSDEConns.py file? I did not put this in a path where there were spaces but had a len(javaClassLocation)=65 and it worked great — but when I moved everything into our corporate path location (also with no spaces), the len(javaClassLocation)=76 and the script failed with error:
    File “C:FSReferenceGeoToolR10TNFScriptCreateTNF1WayReplicacreateTNFSDEConns.py”, line 127, in
    javaClassLocation)
    File “C:FSReferenceGeoToolR10TNFScriptCreateTNF1WayReplicacreateTNFSDEConns.py”, line 79, in createSDEConnFile
    raise Exception, “$PHU Warning Create of ” + sdeFileName + ” failed”
    Exception: $PHU Warning Create of IWebLibrary.sde failed