ODBC连接到sql server database c ++

时间:2016-09-01 06:06:30

标签: c++ sql sql-server odbc

我遇到了与sql server数据库连接的问题。我的代码如下所示。它是用visual studio c ++控制台应用程序编写的。打印“无法连接”行后程序关闭。连接字符串是否正确?请指教。

using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    cout << "Program had started.." << endl;

    SQLHENV env;
    SQLHDBC dbc;
    SQLHSTMT stmt;
    SQLRETURN ret;
    SQLSMALLINT columns;

    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
    SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
    SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);

    SQLRETURN SR;
    char szDSN[] = "test";
    char szUID[] = "Admin";
    char szAuthStr[] = "password";

    cout << "Attempting Connection " << endl;

    SR = SQLConnect(dbc, (SQLWCHAR*)szDSN, SQL_NTS, (SQLWCHAR*)szUID, SQL_NTS, (SQLWCHAR*)szAuthStr, SQL_NTS);

    cout << "Connecting ... " << endl;

    if (SR!= SQL_SUCCESS && SR != SQL_SUCCESS_WITH_INFO)
    {
        cout << "fail to connect" << endl;

    }
    else
    {
        cout << "connected" << endl;
    }


    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的示例已固定

修复您的示例并将其修改为使用DSN-less connection。 在Linux(fedora 32)上使用g++ code.cpp -std=c++17 -lodbc -o test.out进行了测试。

// https://stackoverflow.com/questions/39263791/odbc-connection-to-sql-server-database-c
// https://www.easysoft.com/developer/languages/c/odbc_tutorial.html

#include <sql.h>
#include <sqlext.h>
#include <iostream>
#include <string>

using namespace std;

void extract_error(
    string fn,
    SQLHANDLE handle,
    SQLSMALLINT type)
{
    SQLINTEGER   i = 0;
    SQLINTEGER   native;
    SQLCHAR      state[ 7 ];
    SQLCHAR      text[256];
    SQLSMALLINT  len;
    SQLRETURN    ret;

    cout << "\nThe driver reported the following diagnostics whilst running " << fn << "\n\n";

    do
    {
        ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
                            sizeof(text), &len );
        if (SQL_SUCCEEDED(ret))
            printf("%s:%ld:%ld:%s\n", state, i, native, text);
    }
    while( ret == SQL_SUCCESS );
}



int main()
{
    cout << "Program had started.." << endl;

    SQLHENV env;
    SQLHDBC dbc;
    SQLRETURN ret;
    SQLCHAR outstr[1024];
    SQLSMALLINT outstrlen;

    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);
    SQLSetEnvAttr(env, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
    SQLAllocHandle(SQL_HANDLE_DBC, env, &dbc);

    SQLRETURN SR;

    cout << "Attempting Connection " << endl;
    SQLCHAR sqlConnectionString [] = "DRIVER={ODBC Driver 17 for SQL Server};SERVER=localhost, 1433;UID=SA;PWD=yourPassword;";
    ret = SQLDriverConnect(dbc, NULL, sqlConnectionString, SQL_NTS,
                         outstr, sizeof(outstr), &outstrlen,
                         SQL_DRIVER_NOPROMPT);

    cout << "Connecting ... " << endl;
    extract_error("SQLDriverConnect", dbc, SQL_HANDLE_DBC);

    if (SR!= SQL_SUCCESS && SR != SQL_SUCCESS_WITH_INFO)
    {
        cout << "fail to connect" << endl;

    }
    else
    {
        cout << "connected" << endl;
    }


    return 0;
}

输出

Program had started..
Attempting Connection 
Connecting ... 

The driver reported the following diagnostics whilst running SQLDriverConnect

01000:1:5701:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed database context to 'master'.
01000:2:5703:[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Changed language setting to us_english.
connected