C ++ ODBC SQL Server 2008连接

时间:2021-08-01 15:53:05

I'm trying to connect to a local SQL Server 2008 instance from a newly created C++ app. I am still learning C++, so I might be missing something obvious.

我正在尝试从新创建的C ++应用程序连接到本地SQL Server 2008实例。我还在学习C ++,所以我可能会遗漏一些明显的东西。

I am using Microsoft Visual Studio 2010, creating a Win32 Console App as a blank project and adding main.cpp myself.

我正在使用Microsoft Visual Studio 2010,创建一个Win32控制台应用程序作为空白项目并自己添加main.cpp。

The SQL Server instance is up and running, I can connect to it from other machines using either SQL or Integrated Authentication, I can even get my C# applications to connect using the .net objects.

SQL Server实例已启动并运行,我可以使用SQL或集成身份验证从其他计算机连接到它,我甚至可以使用.net对象连接我的C#应用​​程序。

However in C++ I just can't get a connection, it's always reporting as failed.

但是在C ++中,我无法获得连接,它始终报告为失败。

My code so far is as follows

到目前为止,我的代码如下

#include <iostream>
#include <windows.h>
#include <sqltypes.h>
#include <sql.h>
#include <sqlext.h>

int main() {

    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret;
    SQLSMALLINT columns; 
    int row = 0;

    /* Allocate an environment handle */
    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
    /* We want ODBC 3 support */
    SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void *) SQL_OV_ODBC3, 0);
    /* Allocate a connection handle */
    SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);

    /* Connect to the DSN */
    SQLDriverConnectW(dbc, NULL, L"DRIVER={SQL Server};SERVER=(local)\DB1;DATABASE=master;UID=sa;PWD=password;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);

    /* Check for success */
    if(SQL_SUCCESS!=SQLAllocHandle(SQL_HANDLE_STMT, dbc, &stmt))
    {
        std::cout << "Failed to connect";
    }

    std::cin.get();
    return 0;
}

I've based this on seeing a few online examples such as http://www.easysoft.com/developer/languages/c/odbc_tutorial.html

我的基础是看到一些在线示例,例如http://www.easysoft.com/developer/languages/c/odbc_tutorial.html

Always, my code is entering to Failed to connect block, even with valid credentials. And trying to use extract_error from the linked article above, produces me no error message at all.

总是,我的代码进入无法连接块,即使有效凭据也是如此。并尝试使用上面链接文章中的extract_error,根本不会产生任何错误消息。

What am I doing wrong here please? Thanks

我在这做错了什么?谢谢

1 个解决方案

#1


4  

Reposting from comments so question can be closed:

从评论中重新发布,可以关闭问题:

  • (SQLWCHAR*) is a glaring error. Try L instead to make your string literal a wide string literal.

    (SQLWCHAR *)是一个明显的错误。尝试L而不是使您的字符串文字成为一个宽字符串文字。

  • The default connection timeout is 60 seconds, so you're probably running into that. Check your SQL protocol settings, and if it's configured to use TCP locally, check your firewall settings as well.

    默认连接超时为60秒,因此您可能遇到了这种情况。检查SQL协议设置,如果配置为在本地使用TCP,请检查防火墙设置。

#1


4  

Reposting from comments so question can be closed:

从评论中重新发布,可以关闭问题:

  • (SQLWCHAR*) is a glaring error. Try L instead to make your string literal a wide string literal.

    (SQLWCHAR *)是一个明显的错误。尝试L而不是使您的字符串文字成为一个宽字符串文字。

  • The default connection timeout is 60 seconds, so you're probably running into that. Check your SQL protocol settings, and if it's configured to use TCP locally, check your firewall settings as well.

    默认连接超时为60秒,因此您可能遇到了这种情况。检查SQL协议设置,如果配置为在本地使用TCP,请检查防火墙设置。