RODBC - 连接字符串添加用户名和密码

时间:2018-02-27 17:12:36

标签: r rodbc

我想通过连接字符串连接到SQL服务器。

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)

但我不确定如何添加用户名和密码的详细信息。 connectionString的文档看起来有点模糊。

任何?

2 个答案:

答案 0 :(得分:3)

您可以通过以下方式在连接字符串中添加用户名和密码:

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

答案 1 :(得分:1)

您可以使用glue简化此操作,然后跳过笨拙的paste0步骤。

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};"))