简体   繁体   English

在R中使用DBI:dbConnect连接到SQL Server时出现问题

[英]Issue connecting to SQL Server using DBI:dbConnect in R

The first method I tried works. 我尝试的第一种方法有效。 I first defined an ODBC connection my_connection_name with ODBC data source administrator on my windows. 我首先在Windows上与ODBC data source administrator定义了ODBC连接my_connection_name Then I use the following code: 然后,我使用以下代码:

library(DBI)
library(odbc)
con <- DBI::dbConnect(odbc(),
                      dsn='my_connection_name',
                      UID = "firstname.lastname@company.com",
                      PWD = "mypassword")

This works fine! 这样很好!

However, when I tried to define the driver, server and database name within the dbConnect function. 但是,当我尝试在dbConnect函数中定义驱动程序,服务器和数据库名称时。 It fails! 它失败!

con <- DBI::dbConnect(odbc(),
                      Driver = "ODBC Driver 13 for SQL Server",
                      Server = 'my_server_name',
                      Database = "my_database_name",
                      UID = "firstname.lastname@company.com",
                      PWD = "mypassword",
                      Trusted_Connection = "yes")

I got error: 我收到错误消息:

Error: nanodbc/nanodbc.cpp:950: 08S01: [Microsoft][ODBC Driver 13 for SQL Server]TCP Provider: An existing connection was forcibly closed by the remote host.

Interestingly, when I remove the Trusted_Connection = "yes" . 有趣的是,当我删除Trusted_Connection = "yes"

con <- DBI::dbConnect(odbc(),
                      Driver = "ODBC Driver 13 for SQL Server",
                      Server = 'my_server_name',
                      Database = "my_database_name",
                      UID = "firstname.lastname@company.com",
                      PWD = "mypassword")

I got error message like this: 我收到这样的错误消息:

Error: nanodbc/nanodbc.cpp:950: HY000: [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Cannot open server "company.com" requested by the login. 错误:nanodbc / nanodbc.cpp:950:HY000:[Microsoft] [用于SQL Server的ODBC驱动程序13] [SQL Server]无法打开登录请求的服务器“ company.com”。 The login failed. 登录失败。

I used the same driver, server and database name when I defined the ODBC connection previously. 之前定义ODBC连接时,我使用了相同的驱动程序,服务器和数据库名称。

My authentication option is ActiveDirectoryPassword . 我的身份验证选项是ActiveDirectoryPassword

Does anyone know what is going on? 有人知道发生了什么吗?

if using Trusted Connection = Yes, you don't need to supply your username and password. 如果使用Trusted Connection =是,则无需提供用户名和密码。 your service account will be used to connect to your sql server. 您的服务帐户将用于连接到SQL Server。

con <- DBI::dbConnect(odbc(),
                      Driver = "ODBC Driver 13 for SQL Server",
                      Server = 'my_server_name',
                      Database = "my_database_name",                    
                      Trusted_Connection = "yes")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM