ODBC连接到sql server数据库c++

ODBC connection to sql server database c++

本文关键字:数据库 c++ server sql 连接 ODBC      更新时间:2023-10-16

我有连接到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;
}

你的例子

修复你的例子,并修改它使用一个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 << "nn";
    do
    {
        ret = SQLGetDiagRec(type, handle, ++i, state, &native, text,
                            sizeof(text), &len );
        if (SQL_SUCCEEDED(ret))
            printf("%s:%ld:%ld:%sn", 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