我想使用 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.
我认为用户名中的
@
的表现并不像我最初预期的那样。我该如何让这个连接发挥作用? - 我想我只需要找到正确的格式来表达(转义?)用户名。
这对我有用
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'))
这对我有用:
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)