ODBC连接sql server数据库c++

问题描述 投票:0回答:2

我在连接 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;
}
c++ sql sql-server odbc
2个回答
2
投票

你的例子已修复

修复了您的示例并将其修改为使用

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

0
投票

菜鸟问题。我如何为此设置一个cmake?

© www.soinside.com 2019 - 2024. All rights reserved.