private void DisconnectActiveSDEConnections(IGxApplication gxApp, bool useSelectedFolder = false, string folderName = "Database Connections")
        {
            IGxCatalog gxCatalog = gxApp.Catalog;
            object connectionsFolder = null;
            IGxSelection gxSel = null;
            IGxObject gxObj = null;
            int numFound;
            try
            {
                if (useSelectedFolder)
                {
                    gxSel = gxApp.Selection;
                    gxObj = gxSel.Location;
                }
                else
                {
                    connectionsFolder = gxCatalog.GetObjectFromFullName(folderName, out numFound);
                    if (numFound != 1 || connectionsFolder == null)
                    {
                        MessageBox.Show("Folder not found!");
                        return;
                    }
                    gxObj = connectionsFolder as IGxObject;
                }
                if (gxObj is IGxObjectContainer)
                {
                    IGxObjectContainer gxObjCont = null;
                    IEnumGxObject gxEnum = null;
                    IGxObject gxObject = null;
                    IGxDatabase2 gxDatabase = null;
                    gxObjCont = gxObj as IGxObjectContainer;
                    gxEnum = gxObjCont.Children; //enumerate folder contents
                    gxObject = gxEnum.Next();
                    while (gxObject != null)
                    {
                        gxDatabase = gxObject as IGxDatabase2;
                        Debug.Print(gxObject.Category);
                        if (gxDatabase != null && gxDatabase.IsConnected)
                        {
                            gxDatabase.Disconnect();
                              Debug.Print(gxObject.FullName + " Disconnected");
                        }
                        gxObject = gxEnum.Next();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
Ahmed,
I tried that solution and it worked perfectly. It did exactly what I wanted. Thank you for sharing with me.
Dave
Anyone figured out a way to do this in python instead?
What if you try it in Python in a 'with' environment? Files and alike are usually closed when leaving the environment. Close is basically invoked.
Don't know if it works with an SDE connection the same way...
DisconnectUser (arcpy):
Thanks - yes, I had found this solution for the actual disconnect,, but I have 30 databases. I would like to be able to cycle through a list of connection files in a folder and disconnect all users in each connection file prior to upgrading and restarting servers, etc. I had an ArcSDE command line to do this on Unix, but since that is going away I'd like to replace it with something I can mange with Python that can do both SQL Server and Oracle instances.. Otherwise I may have to use Poewershell to manage this portion of the script...just wondering if anyone else had worked this out yet...
By following the steps in this post I can run my ArcObjects code inside a python script from Arcmap,
I just rewrite part of the disconnect snippet as a POC and tested it from Arcmap python window and from toolbox and it woks fine.
def ArcCatalog_DisconnectSDEConnections(): GetDesktopModules() pApp = GetCurrentApp() import comtypes.gen.esriFramework as esriFramework import comtypes.gen.esriCatalogUI as esriCatalogUI import comtypes.gen.esriCatalog as esriCatalog pGxApp = CType(pApp, esriCatalogUI.IGxApplication) print pGxApp pGxObj = pGxApp.SelectedObject if not pGxObj: print "Nothing selected." return pGxContainer = CType(pGxObj, esriCatalog.IGxObjectContainer) if not pGxContainer: print "No folder selected." return pgxEnum =CType(pGxContainer.Children,esriCatalog.IEnumGxObject) gxObject =CType(pgxEnum.Next(),esriCatalog.IGxObject) while gxObject: gxDatabase = CType(gxObject,esriCatalog.IGxDatabase) #print gxDatabase.IsConnected if gxDatabase and gxDatabase.IsConnected: gxDatabase.Disconnect() print gxObject.FullName + " Disconnected" gxObject =CType(pgxEnum.Next(),esriCatalog.IGxObject)
I'm using Arcmap 10.2.1, I added the code to the 10.2 snippets file and called it directly after importing the file.
import arcpy import sys sys.path.append(r'FOLDER_CONTAINS_SNIPPET_FILE') from Snippets import ArcCatalog_DisconnectSDEConnections try: ArcCatalog_DisconnectSDEConnections() except: arcpy.AddMessage(sys.exc_info())
You should check "Run Python script in process" checkbox in script properties to get it works.
Hope this approach helps.
That’s great, thank you!!!
Tracie
Tracie Streltzer
GIS Application Administrator - Lead
South Florida Water Management District
561.682.6134 | tstrelt@sfwmd.gov<mailto:tstrelt@sfwmd.gov>