C ++为什么在尝试连接数据库时在ntdll.dll中出现堆损坏错误?

时间:2019-02-06 06:32:38

标签: c++ sql-server

当我尝试初始化或建立与数据库的连接时,ntdll中发生堆损坏错误。错误会随机出现在SQLAllocHandle,SQLSetEnvAttr,SQLAllocHandle方法中。

我注意到在Microsoft SQL Managment Studio中检查表时出现错误。一旦我在SQL Studio中断开连接,错误就会消失。

SQLHANDLE SQLEnVHandle = NULL;
    SQLHANDLE SQLConnectionHandle = NULL;
    SQLHANDLE SQLStatementHandle = NULL;
    SQLRETURN retCode = 0;

    do {
        if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &SQLEnVHandle))
            break;
        if (SQL_SUCCESS != SQLSetEnvAttr(SQLEnVHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0))
            break;
        if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, SQLEnVHandle, &SQLConnectionHandle))
            break;
        if (SQL_SUCCESS != SQLSetConnectAttr(SQLConnectionHandle, SQL_LOGIN_TIMEOUT, (SQLPOINTER)5, 0))
            break;

        //Data Source=RADIO-DB2\RADIODB2;Initial Catalog=mdp;Integrated Security=True
        SQLCHAR retConString[1024];
        switch (SQLDriverConnect(SQLConnectionHandle, NULL, (SQLCHAR*)"DRIVER={SQL Server}; SERVER=RADIO-DB2\\RADIODB2; DATABASE = mdp;",
            SQL_NTS, retConString, 1024, NULL, SQL_DRIVER_NOPROMPT))
        {
        case SQL_SUCCESS:
            break;
        case SQL_SUCCESS_WITH_INFO:
            break;
        case SQL_NO_DATA_FOUND:
            showSQLError(SQL_HANDLE_DBC, SQLConnectionHandle);
            retCode - 1;
            break;
        case SQL_INVALID_HANDLE:
            showSQLError(SQL_HANDLE_DBC, SQLConnectionHandle);
            retCode - 1;
            break;
        case SQL_ERROR:
            showSQLError(SQL_HANDLE_DBC, SQLConnectionHandle);
            retCode - 1;
            break;
        default:
            break;
        }

        if (retCode == -1)
            break;

        if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, SQLConnectionHandle, &SQLStatementHandle))
            // Allocates the statement
            break;
} while (FALSE);

我尝试了堆调试,但无济于事。

0 个答案:

没有答案