使用 R 中的 DBI 和 ActiveDirectoryPassword 身份验证连接到 Azure SQL 数据库

问题描述 投票:0回答:2

我想使用 R 中的 DBI 连接到 AzureSQL 数据库。我想通过 AzureAD

ActiveDirectoryPassword
身份验证管理此连接。这需要我说出 AzureAD
UID
Password

我已验证我可以使用其他身份验证方法连接到此服务器。

运行下面的代码会导致错误:

DBI::dbConnect(
            odbc()
            , Driver = "SQL Server"
            , .connection_string = 
                    sprintf("server=%s;database=dbName;UID=%s;PWD=%s;Authentication=%s;"
                            , 'sqlservername.database.windows.net'
                            , '[email protected]'
                            , pwd # Let's just assume this is securely done for now
                            , 'ActiveDirectoryPassword'))

预期结果是连接数据库

实际结果如下:

Error: nanodbc/nanodbc.cpp:950: HY000: [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open server "contoso.com" requested by the login.  The login failed.

我认为用户名中的

@
的表现并不像我最初预期的那样。我该如何让这个连接发挥作用? - 我想我只需要找到正确的格式来表达(转义?)用户名。

r azure odbc azure-sql-database dbi
2个回答
0
投票

这对我有用

DBI::dbConnect(
            odbc()
            , Driver = "SQL Server"
            , .connection_string = 
                    sprintf("server=%s;database=%s;UID=%s;PWD=%s;Authentication=%s;"
                            , 'sqlservername.database.windows.net'
                            , 'databasename'
                            , '[email protected]'
                            , pwd # Let's just assume this is securely done for now
                            , 'ActiveDirectoryPassword'))

0
投票

这对我有用:

dbConnect(odbc::odbc(), 
                 Driver = "ODBC Driver 18 for SQL Server",
                 server = "server", 
                 Authentication = "ActivedirectoryPassword",
                 database = "database", 
                 UID = uid,
                 PWD = rstudioapi::askForPassword("Microsoft password"),
                 Port = "port", 
                 timeout = 30)
© www.soinside.com 2019 - 2024. All rights reserved.