Handy utility to build a connection string to pass into DBI::dbConnect. Accepts trusted connections or username/password.

build_connection_string(
  server,
  driver = "SQL Server",
  database,
  trusted = TRUE,
  user_id,
  password
)

Arguments

server

A string, quoted, required. The name of the server you are trying to connect to.

driver

A string, quoted, optional. Defaults to "SQL Server", but use any driver you like.

database

A string, quoted, optional. If provided, connection string will include a specific database. If NA (default), it will connect to master and you'll have to specify the database when running a query.

trusted

Logical, optional, defaults to TRUE. If FALSE, you must use a user_id and password.

user_id

A string, quoted, optional. Don't include if using trusted.

password

A string, quoted, optional. Don't include if using trusted.

Value

A connection string

See also

Examples

if (FALSE) { my_con <- build_connection_string(server = "localhost") con <- DBI::dbConnect(odbc::odbc(), .connection_string = my_con) # with username and password my_con <- build_connection_string(server = "localhost", user_id = "jules.winnfield", password = "pathoftherighteous") con <- DBI::dbConnect(odbc::odbc(), .connection_string = my_con) }