SQL连接与C ++错误提取

时间:2014-08-29 13:34:05

标签: c++ sql sql-server

所以,我得到了一个简单的连接到SQL Server,它连接正常,并且执行sql也很好,它返回第一行,但是当它再次点击Fetch时会抛出这个错误:

Unhandled exception at 0x011219A0 in JangadaServer.exe: 0xC0000005: Access violation reading location 0x00000008.

代码:

bool database::connect()
{
    if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlenvhandle))
        return false;
    if (SQL_SUCCESS != SQLSetEnvAttr(sqlenvhandle, SQL_ATTR_ODBC_VERSION,     (SQLPOINTER)SQL_OV_ODBC3, 0))
        return false;
    if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_DBC, sqlenvhandle, &sqlconnectionhandle))
        return false;
    SQLWCHAR retconstring[1024];
    switch (SQLDriverConnect(sqlconnectionhandle,
        NULL,
        (SQLWCHAR*)TEXT("DRIVER=SQL       Server;SERVER=.\\SQLEXPRESS;DATABASE=jangadares;UID=sa;PWD=******;Trusted_Connection=no;"),
        SQL_NTS,
        retconstring,
        1024,
        NULL,
        SQL_DRIVER_NOPROMPT)){
    case SQL_SUCCESS_WITH_INFO:
        show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
        break;
    case SQL_INVALID_HANDLE:
    case SQL_ERROR:
        show_error(SQL_HANDLE_DBC, sqlconnectionhandle);
        return false;
    default:
        break;
    }
    return true;
}

bool database::verify_account(char* account, char* password)
{
    if (SQL_SUCCESS != SQLAllocHandle(SQL_HANDLE_STMT, sqlconnectionhandle, &sqlstatementhandle))
        return false;
    if (SQL_SUCCESS != SQLExecDirect(sqlstatementhandle, (SQLWCHAR*)TEXT("select * from accounts"), SQL_NTS)){
        show_error(SQL_HANDLE_STMT, sqlstatementhandle);
        return false;
    }
    else{
        char account[50];
        char password[50];
        int id;
        while (SQLFetch(sqlstatementhandle) == SQL_SUCCESS){
            SQLGetData(sqlstatementhandle, 1, SQL_C_UBIGINT, &id, 0, NULL);
            SQLGetData(sqlstatementhandle, 2, SQL_C_CHAR, account, 50, NULL);
            SQLGetData(sqlstatementhandle, 3, SQL_C_CHAR, password, 50, NULL);
            std::string acc = account;
            acc.erase(remove_if(acc.begin(), acc.end(), isspace), acc.end());
            std::string pwd = password;
            pwd.erase(remove_if(pwd.begin(), pwd.end(), isspace), pwd.end());
            printf("%d", id);
            printf(acc.c_str());
            printf(pwd.c_str());
        }
    }
    return false;
}

我不知道该怎么做,因为它返回第一行没关系,而且它只有一行,所以我想我应该处理这些错误或者是什么?

0 个答案:

没有答案