MyNixOS website logo
Description

Simple Helpers for Connecting to 'SQL Server'.

Lightweight helpers for connecting to Microsoft 'SQL Server' using 'DBI', 'odbc', and 'pool'. Provides simple wrappers for building connection arguments, establishing connections, and safely disconnecting.

sqlserverconnect

Contents

sqlserverconnect provides a minimal, user-friendly interface for connecting to Microsoft SQL Server from R.

It wraps DBI (with the odbc driver) and optionally pool with a small set of consistent helpers:

  • db_connect() – create a DBI connection (default) or a connection pool
  • db_disconnect() – safely close either a DBI connection or a pool

The goal is to offer a lightweight API without the repeated setup/cleanup boilerplate that shows up in scripts and Shiny apps.

Installation

You can install the development version of sqlserverconnect from GitHub:

# install.packages("remotes")
remotes::install_github("drosenman/sqlserverconnect")

Quick start

Windows Authentication (trusted connection)

When using Windows Authentication, you typically don’t need uid/pwd. Keep trusted = TRUE (the default).

library(sqlserverconnect)
library(DBI)

conn <- db_connect(
  server   = "localhost",
  database = "master"
)

DBI::dbGetQuery(conn, "SELECT TOP (5) name, create_date FROM sys.databases")

db_disconnect(conn)

Username + password authentication

For SQL authentication, set trusted = FALSE and provide uid and pwd.

Tip: avoid hardcoding passwords in scripts. Use environment variables, a keyring, or another secret manager.

library(sqlserverconnect)
library(DBI)

conn <- db_connect(
  server   = "localhost",
  database = "master",
  uid      = Sys.getenv("SQLSERVER_UID"),
  pwd      = Sys.getenv("SQLSERVER_PWD"),
  trusted  = FALSE
)

DBI::dbGetQuery(conn, "SELECT TOP (5) name FROM sys.tables")

db_disconnect(conn)

Pooled connections

db_connect() supports pooled connections via the pool package. Set pool = TRUE to create a pool, or leave it as the default (FALSE) for a regular DBI connection.

library(sqlserverconnect)
library(DBI)

pool <- db_connect(
  server   = "localhost",
  database = "master",
  pool     = TRUE
)

DBI::dbGetQuery(pool, "SELECT TOP (5) name FROM sys.databases")

db_disconnect(pool)

DBI vs pool: when to use which?

  • Use a plain DBI connection (pool = FALSE) for interactive scripts and short-lived jobs.
  • Use a pool (pool = TRUE) for Shiny apps or long-running processes where you want connections managed and reused.
Feature / Use casedb_connect(pool = FALSE)db_connect(pool = TRUE)
Interactive scriptsSimple and directUsually unnecessary
Long-running jobsMay time out if idleBetter handling of idle / reused conns
Shiny appsRisk of too many connectionsRecommended best practice
Parallel workloadsEach worker opens its own connPool can reuse connections (per process)
Cleanupdb_disconnect()db_disconnect()

Shiny Use

In Shiny, create the pool once (at startup), reuse it everywhere, and close it when the app stops.

# global.R (or at the top of app.R)
library(sqlserverconnect)

db_pool <- db_connect(
  server   = "localhost",
  database = "master",
  pool     = TRUE
)

onStop(function() {
  db_disconnect(db_pool)
})

Why use sqlserverconnect?

  • Minimal surface area (two main functions)
  • Clear, explicit arguments
  • Works with either a DBI connection or a pool
  • Reduces boilerplate while staying close to DBI/odbc/pool

If you frequently connect to SQL Server from R, this package keeps your workflow clean and consistent.

Built on

sqlserverconnect is built on these packages:

  • DBI – provides the database interface and dbConnect() generic
  • odbc – provides the ODBC driver (odbc::odbc()) used by DBI to talk to SQL Server
  • pool – optional connection pooling for DBI connections (recommended for Shiny apps)
Metadata

Version

0.1.0

License

Unknown

Platforms (78)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows