Show all open RODBC connections

谁都会走 提交于 2019-12-19 04:18:22

问题


Does anyone know how to do this? showConnections won't list any open connections from odbcConnect.


回答1:


You can narrow down your search in the following way, which will return all variables of your environment currently of the class RODBC.

envVariables<-ls()
bools<-sapply(envVariables, function(string){
        class(get(string))=="RODBC"
    })

rodbcObj<-envVariables[bools]

Closed connections are still of the class RODBC though, so there is still a little work to be done here.

We can define a function, using trycatch, that will try to get the connection info of the associated RODBC object. If it is an open connection, then this command will run fine, and we return the string of the variable name.

If the RODBC object is not an open connection, this will throw an error, which we catch and, in the way I've implemented, return NA. You could return any number of things here.

openConns<-function(string){

   tryCatch({
            result<-odbcGetInfo(get(string))
            string
        }, error = function(e){
            NA
        })
}

We then remove the return value that corresponds to the error. In my case, NA, so I do na.omit on the return.

na.omit(sapply(rodbcObj, openConns))

Or alternatively

result<-sapply(rodbcObj, openConns)
result[!is.na(result)]

Any questions or comments on it let me know

-DMT



来源:https://stackoverflow.com/questions/25530363/show-all-open-rodbc-connections

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!