Connect to MSSQL using DBI

indra_patil picture indra_patil · Jul 21, 2014 · Viewed 7.8k times · Source

I can not connect to MSSQL using DBI package. I am trying the way shown in package itself

    m <- dbDriver("RODBC") # error

Error: could not find function "RODBC"

    # open the connection using user, passsword, etc., as
    # specified in the file \file{\$HOME/.my.cnf}
    con <- dbConnect(m, dsn="data.source", uid="user", pwd="password"))    

Any help appreciated. Thanks

Answer

dougmet picture dougmet · Mar 14, 2017

As an update to this question: RStudio have since created the odbc package (or GitHub version here) that handles ODBC connections to a number of databases through DBI. For SQL Server you use:

con <- DBI::dbConnect(odbc::odbc(),
                      driver = "SQL Server",
                      server = <serverURL>,
                      database = <databasename>,
                      uid = <username>,
                      pwd = <passwd>)

You can also set a dsn or supply a connection string.