Prometheus
Prometheus

Reputation: 2017

RODBC - connection string add username and password

I want to connect though a connection string to a SQL server.

library(RODBC)
server <- "servername"
database<- "db_name"
username <- "MyId"
password <- "MyPassword"
connectionString <- paste0("Driver={SQL Server};server=",server,";database=",database,";trusted_connection=yes;")
channel <-  odbcDriverConnect(connection=connectionString)

But I'm not sure how to add the details for username and password. The documentation for the connectionString seems a little bit vague.

Anyone?

Upvotes: 1

Views: 5123

Answers (2)

camnesia
camnesia

Reputation: 2323

You can make this simpler by using glue, then you can skip the awkward paste0 step.

library(glue)
library(RODBC)

server <- "servername"
database<- "db_name"
username <- "MyId"
password <- "MyPassword"

channel <- odbcDriverConnect(glue("driver=SQL Server; 
                                   server={server}; 
                                   database={database}; 
                                   uid={username}; 
                                   pwd={password};"))

Upvotes: 1

sm925
sm925

Reputation: 2678

You can add username and password in a connection string this way:

connectionString <- paste0("DRIVER={SQL Server}; server=",server,"; database=",db_name,"; uid=",username,"; pwd=",MyPassword, sep="")

Upvotes: 3

Related Questions