0

我正在尝试将 R 与 SQL Server 2016 连接,当我尝试RevoScaleR_SqlServer_GettingStarted.R脚本或我自己的脚本时,出现以下错误:

[Microsoft][ODBC 驱动程序管理器] 未找到数据源名称且未指定默认驱动程序 SQLDisconnect 中的 ODBC 错误无法打开数据源。doTryCatch(return(expr), name, parentenv, handler) 中的错误:无法打开数据源。

注意:我可以在 SQL 管理工作室中执行 R 脚本。我已经在 ODBC 数据源(64 位)中为 SQL Server 版本 13.00.1300 配置了 Microsoft ODBC 驱动程序。

4

3 回答 3

0

我刚刚完成了对相同错误消息的故障排除。

“未找到数据源名称且未指定默认驱动程序”表示 dsn 和驱动程序需要包含在连接详细信息中。

以下来自 rstudio 的文章很有帮助,这对我有用:https: //support.rstudio.com/hc/en-us/articles/214510788-Setting-up-R-to-connect-to-SQL-Server -

library(DBI)
con2 <- dbConnect(odbc::odbc(),
                 .connection_string = "Driver={Simba SQL Server ODBC Driver};",
                 server= "<server>", 
                 dsn = "<data source name>",
                 database = "<database>",
                 uid = rstudioapi::askForPassword(prompt = 'Please enter username: '),
                 pwd = rstudioapi::askForPassword("Database password"),
                 timeout = 10,
                 trusted_connection = TRUE)

除了正确连接之外,还需要正确设置 ODBC 管理器,并且需要选择/安装适用于 SQL Server 的驱动程序。

于 2020-04-23T14:22:11.953 回答
0

遇到相同的消息...看起来这只是来自 R 的一般消息,表明您的连接字符串有问题。

至少这是我的问题......示例代码中有一个空格,它需要您的服务器实例名称。删除空间为我修复了它。

# https://microsoft.github.io/sql-ml-tutorials/R/customerclustering/step/2.html
#Connection string to connect to SQL Server. Don't forget to replace MyServer with the name of your SQL Server instance

connStr <- paste("Driver=SQL Server;Server=", " localhost", ";Database=" , "tpcxbb_1gb" , ";Trusted_Connection=true;" , sep="" ); # Broken... due to space in the paste.
connStr <- paste("Driver=SQL Server;Server=", "localhost", ";Database=" , "tpcxbb_1gb" , ";Trusted_Connection=true;" , sep="" ); #Fixed.
于 2017-11-12T20:18:54.807 回答
-1

可能您应该在 r wd 中创建一个 txt 文件并将连接字符串保存在其中,之后您应该使用 readLines("your connection string.txt") 读取连接字符串并在您的代码中使用它为我工作...... ..仅供参考,您应该禁用从具有高级安全性的Windows防火墙中阻止R....

于 2016-12-13T05:37:48.710 回答