简体   繁体   English

通过 C++ 中的 ODBC 连接到 Azure Z9778840A0100CB305C982876BA24 数据库时出现问题

[英]Issue in connecting via ODBC in C++ to Azure SQL database

This is my first question on StackOverflow, so please excuse my innocence!这是我在 StackOverflow 上的第一个问题,请原谅我的无辜!

Here's the issue: I'm trying to connect via ODBC on Windows in C++ to an Azure SQL database, but without success as I keep getting the following message: Here's the issue: I'm trying to connect via ODBC on Windows in C++ to an Azure SQL database, but without success as I keep getting the following message:

[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0)

I retrieved the connection string from Azure and made sure my IP address is registered in the firewall settings.我从 Azure 检索了连接字符串,并确保我的 IP 地址已在防火墙设置中注册。

Following the example provided by Microsoft, I'm connecting thru:按照 Microsoft 提供的示例,我通过以下方式连接:

    _retcode = SQLDriverConnect(
        _hDbc,
        NULL,
        ( SQLCHAR * ) connectionString,
        SQL_NTS,
        NULL,
        0,
        NULL,
        SQL_DRIVER_NOPROMPT );

where connectionString is defined as:其中connectionString定义为:

const char * connectionString = "Driver = { ODBC Driver 13 for SQL Server };"
    "Server = tcp:<datasource>.database.windows.net, 1433;"
    "Database = <database>;"
    "Uid = <account>; Pwd = <password>;"
    "Encrypt = yes; TrustServerCertificate = no; Connection Timeout = 30;";

Also, I tried to connect via .NET in C# and it works with the following connection string build:另外,我尝试通过 C# 中的 .NET 进行连接,它适用于以下连接字符串构建:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
    builder.DataSource = "<datasource>.database.windows.net";
    builder.UserID = "<account>";
    builder.Password = "<password>";
    builder.InitialCatalog = "<datavase>";

I was able to perform the "SELECT @@VERSION" statement with the following result:我能够执行“SELECT @@VERSION”语句,结果如下:

Microsoft SQL Azure (RTM) - 12.0.2000.8
    Feb 26 2020 10:26:43
    Copyright (C) 2019 Microsoft Corporation

So what am I doing wrong???那我做错了什么???

Thank you in advance for your help!预先感谢您的帮助!

I found the solution, First, I provided the OutConnectionString information to display the working connection string.我找到了解决方案,首先,我提供了 OutConnectionString 信息以显示工作连接字符串。 if any.如果有的话。

       SQLDriverConnect(hDbc,
                     GetDesktopWindow(),
                     pwszConnStr,
                     (SQLSMALLINT)wcslen(pwszConnStr),
                     OutConnectionString,
                     BufferLength,
                     &StringLength2Ptr,
                     SQL_DRIVER_COMPLETE));

Then, I reverted to create a DSN file using the wizard that is launched when no command arguments are provided (in the Microsoft sample).然后,我恢复使用在未提供命令 arguments 时启动的向导创建 DSN 文件(在 Microsoft 示例中)。

I managed to provide the right arguments and was able to connect to my Azure SQL database.我设法提供了正确的 arguments 并能够连接到我的 Azure SQL 数据库。

I printed the OutConnectionString and here's the result (quite far from what I was expecting):我打印了 OutConnectionString,这是结果(与我的预期相去甚远):

const char * connectionString = "DRIVER=ODBC Driver 17 for SQL Server;SERVER=<server>.database.windows.net;UID=<account>;PWD=<password>;Trusted_Connection=No;DATABASE=<database>;";

I then inserted the string above directly into my code, this time without a prompt:然后我将上面的字符串直接插入到我的代码中,这次没有提示:

    _retcode = SQLDriverConnect(
    _hDbc,
    NULL,
    ( SQLCHAR * ) connectionString,
    strlen( connectionString ),
    NULL,
    0,
    NULL,
    SQL_DRIVER_NOPROMPT );

And it's working!它正在工作!

For reference, here are the links to the official Microsoft documentation:作为参考,这里是微软官方文档的链接:

Connect to SQL Database using C and C++ 使用 C 和 C++ 连接到 SQL 数据库

SQLDriverConnect Function SQLDriverConnect Function

Connect to an ODBC Data Source (SQL Server Import and Export Wizard) 连接到 ODBC 数据源(SQL Server 导入和导出向导)

Windows with C++: Using Databases on Windows Azure Windows 与 C++:在 Windows Z3A580F142203677F1F0BC30898F63F5 上使用数据库

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

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