| Title: | Simple Helpers for Connecting to 'SQL Server' |
|---|---|
| Description: | 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. |
| Authors: | Dave Rosenman [aut, cre] |
| Maintainer: | Dave Rosenman <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-24 09:22:30 UTC |
| Source: | https://github.com/cranhaven/cranhaven.r-universe.dev |
Connect to a SQL Server database
db_connect( server, database, uid = NULL, pwd = NULL, port = NULL, trusted = TRUE, driver = "ODBC Driver 17 for SQL Server", pool = FALSE, quiet = FALSE, ... )db_connect( server, database, uid = NULL, pwd = NULL, port = NULL, trusted = TRUE, driver = "ODBC Driver 17 for SQL Server", pool = FALSE, quiet = FALSE, ... )
server |
SQL Server hostname, IP address, or instance name |
database |
Database name |
uid |
Username (ignore/keep as NULL if trusted = TRUE) |
pwd |
Password (ignore/keep as NULL if trusted = TRUE) |
port |
Optional port number. If NULL, odbc package handles port resolution |
trusted |
(logical) If TRUE (default), uses Windows authentication |
driver |
ODBC driver name (default is "ODBC Driver 17 for SQL Server") |
pool |
(logical) if TRUE, returns a pooled connection |
quiet |
(logical) if TRUE, suppresses messages |
... |
Additional arguments passed to DBI::dbConnect or pool::dbPool |
A DBI connection or a pool object
# Connect to a SQL Server database conn <- db_connect( server = "localhost", database = "master", quiet = TRUE ) # Run a simple query DBI::dbGetQuery(conn, "SELECT name FROM sys.databases") # Disconnect when finished db_disconnect(conn)# Connect to a SQL Server database conn <- db_connect( server = "localhost", database = "master", quiet = TRUE ) # Run a simple query DBI::dbGetQuery(conn, "SELECT name FROM sys.databases") # Disconnect when finished db_disconnect(conn)
Build SQL Server connection arguments
db_connection_args( server, database, uid = NULL, pwd = NULL, port = NULL, trusted = TRUE, driver = "ODBC Driver 17 for SQL Server" )db_connection_args( server, database, uid = NULL, pwd = NULL, port = NULL, trusted = TRUE, driver = "ODBC Driver 17 for SQL Server" )
server |
SQL Server hostname, IP address, or instance name |
database |
Database name |
uid |
Username (ignore/keep as NULL if trusted = TRUE) |
pwd |
Password (ignore/keep as NULL if trusted = TRUE) |
port |
Optional port number. If NULL, odbc package handles port resolution |
trusted |
(logical) If TRUE (default), uses Windows authentication |
driver |
ODBC driver name (default is "ODBC Driver 17 for SQL Server") |
A named list of arguments suitable for a SQL Server connection
string in DBI::dbConnect() or pool::dbPool(). Used internally by
db_connect() to construct the argument list.
# Build arguments using Windows authentication db_connection_args( server = "localhost", database = "master" ) # Build arguments using SQL authentication db_connection_args( server = "localhost", database = "master", uid = "sa", pwd = "password", trusted = FALSE )# Build arguments using Windows authentication db_connection_args( server = "localhost", database = "master" ) # Build arguments using SQL authentication db_connection_args( server = "localhost", database = "master", uid = "sa", pwd = "password", trusted = FALSE )
Disconnect from a SQL Server connection or pool
db_disconnect(conn, quiet = FALSE)db_disconnect(conn, quiet = FALSE)
conn |
A DBI connection or a pool connection object |
quiet |
(logical) if TRUE, suppresses messages |
TRUE (invisibly) if disconnected, FALSE otherwise
# Establish a connection conn <- db_connect( server = "localhost", database = "master", quiet = TRUE ) # Disconnect when finished db_disconnect(conn) # Disconnecting a NULL connection db_disconnect(NULL)# Establish a connection conn <- db_connect( server = "localhost", database = "master", quiet = TRUE ) # Disconnect when finished db_disconnect(conn) # Disconnecting a NULL connection db_disconnect(NULL)