SQL Server RODBC Connection

后端 未结 5 529
滥情空心
滥情空心 2020-11-28 03:10

Does anyone have a connection string example for using RODBC and connecting to MS SQL Server 2005 or 2008.

Thank you.

相关标签:
5条回答
  • 2020-11-28 03:34

    Taken from a posting to r-help:

     library(RODBC)
     channel <- odbcDriverConnect("driver=SQL Server;server=01wh155073")
     initdata<- sqlQuery(channel,paste("select * from  test_DB .. 
     test_vikrant"))
     dim(initdata)
     odbcClose(channel)
    
    0 讨论(0)
  • 2020-11-28 03:35
    library(RODBC)
    dbhandle <- odbcDriverConnect('driver={SQL Server};server=mysqlhost;database=mydbname;trusted_connection=true')
    res <- sqlQuery(dbhandle, 'select * from information_schema.tables')
    
    0 讨论(0)
  • 2020-11-28 03:42

    Try to use RSQLS package: https://github.com/martinkabe/RSQLS

    Very fast pushes data from data.frame to SQL Server or pulls from SQL Server to data.frame.

    Example:

    library(devtools)
    install_github("martinkabe/RSQLS")
    library(RSQLS)
    
    cs <- set_connString("LAPTOP-USER\\SQLEXPRESS", "Database_Name")
    push_data(cs, dataFrame, "dbo.TableName", append = TRUE, showprogress = TRUE)
    df <- pull_data(cs, "SELECT * FROM dbo.TableName", showprogress = TRUE)
    

    This solution is much faster and more robust than RODBC::sqlSave or DBI::dbWriteTable.

    0 讨论(0)
  • 2020-11-28 03:49

    If you have to include the USERNAME and PASSWORD:

    library(RODBC) # don't forget to install it beforehand
    
    my_server="ABC05"
    my_db="myDatabaseName"
    my_username="JohnDoe"
    my_pwd="mVwpR55zobUldrdtXqeHez"
    
    
    db <- odbcDriverConnect(paste0("DRIVER={SQL Server};
                                     server=",my_server,";
                                     database=",my_db,";
                                     uid=",my_username,";
                                     pwd=",my_pwd))
    
    
    sql="SELECT * FROM dbo.MyTableName" #dbo is the schema here
    df <- sqlQuery(db,sql)
    
    0 讨论(0)
  • 2020-11-28 03:51

    First You have to Create/configure DSN (ODBC connection with specific DB)

    Then install RODBC library.

    library(RODBC)
    myconn <-odbcConnect("MyDSN", uid="***", pwd="*******")
    
    fetchData<- sqlQuery(myconn, "select * from tableName")
    View(fetchData)
    close(myconn)
    
    0 讨论(0)
提交回复
热议问题