Connect azure sql use pyodbc failed

William Wang 1 Reputation point
2022-05-30T07:27:33.287+00:00

We are try to load some data from azure sql to pandas, here are the code:

conn = pyodbc.connect("DRIVER={SQL Server};Server=tcp:xxx.database.windows.net,1433;Initial Catalog=Database;UID=******@xxx.onmicrosoft.com;PWD=xxx;Authentication=ActiveDirectoryPassword;MultipleActiveResultSets=False;")

Then get such error: [HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open server "xxx.onmicrosoft.com" requested by the login. The login failed. (40532) (SQLDriverConnect);

But if I remove the "Authentication=ActiveDirectoryPassword" and use a SQL account it will be success, maybe you will say "Hey, why don't you use SQL authoration, dude." well, considering about some security facts we gonna enable the "Azure AD-only authentication" and the SQL authoration will no longer be available soon.

Azure SQL Database
Microsoft Security Microsoft Entra Microsoft Entra ID
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. William Wang 1 Reputation point
    2022-05-30T08:19:39.43+00:00

    Fixed, need to install the ODBC ver 18 (ver 17 is work or not I'm not sure), and modify the connection str like this:

    conn = pyodbc.connect("DRIVER={ODBC Driver 18 for SQL Server};Server=tcp:xxx.database.windows.net,1433;DATABASE=xxx;UID=******@xxx.onmicrosoft.com;PWD=xxx;Authentication=ActiveDirectoryPassword;")  
      
    
    0 comments No comments

  2. JamesTran-MSFT 36,906 Reputation points Microsoft Employee Moderator
    2022-05-30T17:50:41.163+00:00

    @William Wang
    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "the question author cannot accept their own answer. They can only accept answers by others", I'll repost your solution in case you'd like to "Accept" the answer.

    Solution:
    You'll need to install the ODBC ver 18, and modify the connection str like this:

    conn = pyodbc.connect("DRIVER={ODBC Driver 18 for SQL Server};Server=tcp:xxx.database.windows.net,1433;DATABASE=xxx;UID=******@xxx.onmicrosoft.com;PWD=xxx;Authentication=ActiveDirectoryPassword;")

    Thank you for your time and patience throughout this issue!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.