【问题标题】:Read data from Microsoft SQL database on Remote Desktop via. R通过远程桌面从 Microsoft SQL 数据库读取数据。 R
【发布时间】:2013-12-01 08:30:41
【问题描述】:

我的数据加载到 Microsoft Windows Server 2012 远程桌面上的 Micorsoft SQL 服务器上。我想通过 R 访问数据。我知道它可以通过 RODBC 使用

odbcConnect(dsn, uid="", pwd="")

但我对在 dsn 上输入什么感到困惑。

【问题讨论】:

    标签: sql sql-server r odbc rodbc


    【解决方案1】:

    这取决于您是要使用 ODBC DSN 还是无 DSN 连接字符串、身份验证类型和驱动程序。

    这是一个使用域身份验证和 SQL Server 驱动程序的无 DSN 连接字符串示例:

    odbcDriverConnect(connection="server=MYDB.mynet.tld;database=mydb_prod;trusted_connection=true;Port=1433;driver={SQL Server};TDS_Version=7.0;")
    

    这是另一个示例,使用 SQL 身份验证和 FreeTDS 驱动程序(例如在 Mac 或 Linux 上)

    odbcDriverConnect(connection="server=MYDB.mynet.tld;database=mydb_prod;uid=myuser;pwd=mypass;Port=1433;driver=FreeTDS;TDS_Version=7.0;")
    

    请参阅http://www.connectionstrings.com/sql-server/,了解更多有关“连接”元素的示例。

    这是我用来跨平台打开连接的一个函数:

    require(RODBC)
    
    connect <- function(host, db, user=NULL, pass=NULL, platform="win" ){
    
      # TODO: Check input paramaters and add a branch for SQL auth on windows
    
      if(platform == "win")
      {
        c <- odbcDriverConnect(connection=paste0("server=",host,
                                                 ";database=",db,
                                                 ";trusted_connection=true;Port=1433;driver={SQL Server};TDS_Version=7.0;"))
    
        if(class(c) == 'RODBC') 
        {  
          writeLines("Successfilly opened connection to db")
          return(c)
        } 
        else
        {
          writeLines(paste0("Error opening connection: ", as.character(c)))
        }
      }  
    
      if(platform == "mac")
      {
        c <- odbcDriverConnect(connection=paste0("server=",host,
                                                 ";database=",db,
                                                 ";uid=",user,
                                                 ";pwd=",pass,
                                                 ";Port=1433;driver=FreeTDS;TDS_Version=7.0;"))
    
        if(class(c) == 'RODBC') 
        {  
          writeLines("Successfilly opened connection to db")
          return(c)
        } 
        else
        {
          writeLines(paste0("Error opening connection: ", as.character(c)))
        }
      }  
    }
    

    【讨论】:

    • 当我输入以下作为连接名称时出现错误odbcDriverConnect(connection="server=10.100.0.1;database=db_name;uid=id;pwd=pwd;Port=1433;driver=FreeTDS;TDS_Version=7.0;") 错误Warning messages: [RODBC] ERROR: state IM002, code 0, message [unixODBC][Driver Manager]Data source name not found, and no default driver specified 2:..... ODBC connection failed
    • @FistOfFury 抱歉,我不再从事此工作,忘记了如何解决。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    相关资源
    最近更新 更多