简体   繁体   English

在Linux中使用RODBC连接到SQL Server时出错

[英]Error connecting to SQL Server using RODBC in linux

I'm running different r programs on the linux server. 我在linux服务器上运行不同的r程序。 Programs fail due database connectivity giving following errors:- 程序由于数据库连接失败而出现以下错误:

1: In odbcDriverConnect(paste0("DRIVER={SQL Server}; server=", server, : [RODBC] ERROR: state 08001, code 0, message [unixODBC][FreeTDS][SQL Server]Unable to connect to data source 1:在odbcDriverConnect(paste0(“ DRIVER = {SQL Server}; server =”,server,:[RODBC] ERROR:state 08001,code 0,message [unixODBC] [FreeTDS] [SQL Server]无法连接到数据源

2: In odbcDriverConnect(paste0("DRIVER={SQL Server}; server=", server, : [RODBC] ERROR: state 01000, code 20002, message [unixODBC][FreeTDS][SQL Server]Adaptive Server connection failed 2:在odbcDriverConnect(paste0(“ DRIVER = {SQL Server}; server =”,server,:[RODBC] ERROR:state 01000,code 20002,message [unixODBC] [FreeTDS] [SQL Server] Adaptive Server连接失败

3: In odbcDriverConnect(paste0("DRIVER={SQL Server}; server=", server, : [RODBC] ERROR: state 01000, code 20017, message [unixODBC][FreeTDS][SQL Server]Unexpected EOF from the server 3:在odbcDriverConnect(paste0(“ DRIVER = {SQL Server}; server =”,server ,: [RODBC] ERROR:state 01000,code 20017,message [unixODBC] [FreeTDS] [SQL Server]意外的服务器EOF

4: In odbcDriverConnect(paste0("DRIVER={SQL Server}; server=", server, : ODBC connection failed Execution halted 4:在odbcDriverConnect(paste0(“ DRIVER = {SQL Server}; server =”,server,中:ODBC连接失败执行停止

Sometimes, programs run completely without giving any error. 有时,程序会完全运行而不会出现任何错误。 Sometimes it fails. 有时会失败。 I'm not able to figure out exactly what the problem is. 我无法确切地找出问题所在。

I tested a piece of code to see how it's behaving: 我测试了一段代码,以查看其行为方式:

library(RODBC)

library(RODBCext)



for (i in 1:100){

  dbhandle <- odbcDriverConnect(paste0("DRIVER={SQL Server}; server=",server,"; database=",dbname,"; uid=",username,"; pwd=",password, "; TDS_Version=8.0; Port=1433;", sep=""))

  query <- "select * from analysis"

  analysis <- sqlExecute(dbhandle, query = query, fetch = T)

  odbcClose(dbhandle)


} 

Sometimes it's getting executed completely. 有时它会完全执行。 Sometimes it fails in between(for example it failed 45th time connecting to database) 有时它会在两次之间失败(例如,第45次连接数据库失败)

I never faced this problem in windows environment. 我从未在Windows环境中遇到过此问题。

I want to know what exactly is the problem in this case. 我想知道在这种情况下到底是什么问题。 Is it something related to network, driver, my connection string or database. 它与网络,驱动程序,我的连接字符串或数据库有关。

And what could be the solution to this problem. 而解决这个问题的办法可能是什么。

I guess my connection string and config file looks good. 我猜我的连接字符串配置文件看起来不错。

Maybe there was a network issue which was dropping an open RODBC connection. 可能存在网络问题 ,正在断开开放的RODBC连接。

I tried this solution:- 我尝试了这种解决方案:-

 repeat
        {
          dbhandle <- odbcDriverConnect(paste0("DRIVER={SQL Server}; server=",server,"; database=",dbname,"; uid=",usernm,"; pwd=",passwd, "; TDS_Version=8.0; Port=1433;", sep=""))
          if(class(dbhandle) == "RODBC"){
              break
            }
        }

With this I'm not proceeding till my dbhandle is of class RODBC . 与此相关,直到我的dbhandleRODBC类,我才继续进行。

By integrating this code in my program wherever database connection was required, I was able to run programs without getting those error messages. 通过将此代码集成到需要数据库连接的程序中,我可以运行程序而不会收到这些错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM