Accessing Data in Remote SDE

3049
12
01-20-2014 05:04 PM
KeithSandell1
New Contributor II
I have separate App and DB servers on Azure.

I need to run a python script on the App server that accesses an SDE featureclass on the DB server.

The python script was written on the DB server so it had direct access to SDE, but it needs to reside on the App server.

Basically need to change something like this:

sdeFC = r"C:\SDE_Connection.sde\DB.dbo.FeatureClass"

to something like this:

sdeFC = r"<remote db server>\SDE_Connection.sde\DB.dbo.FeatureClass"


---Suggestions on how to accomplish this?---

Thanks
Tags (2)
0 Kudos
12 Replies
AndrewHaley
New Contributor II
The way i normally do this would be by opening arcCatalog on the App server, and setting up a connection to the SDE that is on the DB server. ( I am assuming they are on the same network?)

That way, your syntax would stay exactly the same. Whatever you name the connection in arcCatalog is what you put into your python script.

For me, it would be:

sdeFC = r"Database Connections\Connection to DB.sde\DB.dbo.FeatureClass"

(the easiest way to get the proper syntax is to set up a dummy modelbuilder tool on the APP server using data from the DB SDE, then exporting to python... copy and paste away :cool: )
0 Kudos
KeithSandell1
New Contributor II
That would be the easiest thing to do, but I can't install Desktop on the App server.

I used arcpy.management.CreateDatabaseConnection to make a connection file on the App server. The script successfully created the .sde file.

To test it I set the connection file as a workspace and ran arcpy.ListFeatureClasses() against it. It did not raise any errors, but it also did not return any featureclasses that are in SDE.
0 Kudos
JamesCrandall
MVP Frequent Contributor
To test it I set the connection file as a workspace and ran arcpy.ListFeatureClasses() against it. It did not raise any errors, but it also did not return any featureclasses that are in SDE.


Can you post this code?  Did you include error checking in a try/except block?
0 Kudos
KeithSandell1
New Contributor II
The code was just something benign like this. I run it from IDLE and it completes without raising an error, but also without printing any featureclass names.

import arcpy

sdeConn = r"<connection file>" ##this is the one that was created with arcpy.management.CreateDatabaseConnection

arcpy.env.workspace = sdeConn

for fc in arcpy.ListFeatureClasses():
     print fc



In the absence of try/except IDLE should raise any error.
0 Kudos
JamesCrandall
MVP Frequent Contributor
The code was just something benign like this. I run it from IDLE and it completes without raising an error, but also without printing any featureclass names.

import arcpy

sdeConn = r"<connection file>" ##this is the one that was created with arcpy.management.CreateDatabaseConnection

arcpy.env.workspace = sdeConn

for fc in arcpy.ListFeatureClasses():
     print fc



In the absence of try/except IDLE should raise any error.


I am unfamiliar with attempting to access items from a particular schema like you are "DB.sde\DB.dbo.FeatureClass".  Have you tried to ListFeatureClasses() with just the .sde connection alone?

sdeFC = r"<remote db server>\SDE_Connection.sde"
0 Kudos
KeithSandell1
New Contributor II
I am unfamiliar with attempting to access items from a particular schema like you are "DB.sde\DB.dbo.FeatureClass".  Have you tried to ListFeatureClasses() with just the .sde connection alone?


What you reference was just an example of accessing a FeatureClass directly, just as you would from a FGDB, i.e. C:\FGDB.gdb\FeatureClass. When you do the same thing in SDE the path includes the DB connection file + the DB + the schema + the featureclass name.

But that is all really irrelevant.

I'm just trying to find a way to have python on one cloud server access an SDE instance on another cloud server, presumably using a connection file I can create on the non-SDE server.

I created a connection file like this and tested it. It worked in the sense that it did not raise an error, but it also did not give me the result I was expecting.
0 Kudos
JamesCrandall
MVP Frequent Contributor
What you reference was just an example of accessing a FeatureClass directly, just as you would from a FGDB, i.e. C:\FGDB.gdb\FeatureClass. When you do the same thing in SDE the path includes the DB connection file + the DB + the schema + the featureclass name.

But that is all really irrelevant.

I'm just trying to find a way to have python on one cloud server access an SDE instance on another cloud server, presumably using a connection file I can create on the non-SDE server.

I created a connection file like this and tested it. It worked in the sense that it did not raise an error, but it also did not give me the result I was expecting.


Right, I assumed you are attempting to access the .ListFeatureClasses() with "DB connection file + the DB" and not "DB connection file + the DB + the schema + the featureclass name".  That is,

Do this:

sdeFC = r"<remote db server>\SDE_Connection.sde"
for fc in arcpy.ListFeatureClasses():
     print fc


Not this:

sdeFC = r"<remote db server>\SDE_Connection.sde\DB.dbo.FeatureClass"
for fc in arcpy.ListFeatureClasses():
     print fc
0 Kudos
KeithSandell1
New Contributor II

Do this:

sdeFC = r"<remote db server>\SDE_Connection.sde"
for fc in arcpy.ListFeatureClasses():
     print fc




I am doing that to read featureclasses from SDE. The script would surely error out if I tried to read featureclasses from something that is not a workspace.

My ultimate goal is not to use ListFeatureClasses(), but to just access a featureclass directly to execute a SearchCursor, etc.

I only used the ListFeatureClasses function to test the connection file.

The nature of the prior example was to differentiate between connecting to a local featureclass and a remote featureclass.

sdeFC = r"C:\SDE_Connection.sde\DB.dbo.FeatureClass"

to something like this:

sdeFC = r"<remote db server>\SDE_Connection.sde\DB.dbo.FeatureClass"


Wherein I need to somehow get my script to see that this data is on a remote server. In theory the connection file would contain this information, but from testing the connection file I do not believe it is working correctly because it is not returning expected results even though it is not erring out.
0 Kudos
MathewCoyle
Frequent Contributor
The script would surely error out if I tried to read featureclasses from something that is not a workspace.


That is half correct. In your case you have created a sde connection file which is a recognized workspace type. Whether or not it actually references a valid database connection is irrelevant to the tool.

Running an arcpy.ListFeatureClasses() on an invalid sde connection will return an empty list.
0 Kudos