如何在c ++

时间:2017-08-28 11:48:42

标签: c++ sql sql-server ascii

我正在使用C ++从SQL Server 2008数据库中读取数据。我可以正常读取普通的ASCII字符。但是,我似乎无法弄清楚如何从数据库中读取韩语,日语或俄语字符。以下是我目前用于执行SQL命令以从数据库中读取字符串的函数:

vector<string> ExSQL(string command) {
    SQLHANDLE sqlConnHandle;
    SQLHANDLE sqlEnvHandle;
    SQLWCHAR retconstring[SQL_RETURN_CODE_LEN];
    sqlConnHandle = NULL;
    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle);
    SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
    SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle);
    SQLDriverConnect(sqlConnHandle,
        NULL,
        (SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=PhraseDB3;Trusted=true;",
        SQL_NTS,
        retconstring,
        1024,
        NULL,
        SQL_DRIVER_NOPROMPT
    );
    vector<string> results;
    SQLHANDLE sqlStmtHandle;
    sqlStmtHandle = NULL;
    SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle);
    wstring widestr = std::wstring(command.begin(), command.end());
    const wchar_t* temp = widestr.c_str();
    SQLExecDirect(sqlStmtHandle, (SQLWCHAR*)temp, SQL_NTS);
    SQLCHAR sqlRes[SQL_RESULT_LEN];
    SQLINTEGER sqlResPtr;
    while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) {
        SQLGetData(sqlStmtHandle, 1, SQL_CHAR, sqlRes, SQL_RESULT_LEN, &sqlResPtr);
        string result((const char*)sqlRes);
        results.push_back(result);
    }
    SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);
    SQLDisconnect(sqlConnHandle);
    SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle);
    SQLFreeHandle(SQL_HANDLE_ENV, sqlEnvHandle);
    return results;
}

此功能适用于普通英语字符串,但不适用于亚洲或俄语字符串(&#34;?&#34;显示每个字符)。我试图用wstring替换这个函数中的字符串,但没有运气。我还将SQLExecDirect更改为SQLExecDirectW。请帮我改变这个功能,以便我可以阅读所有类型的字符。感谢。

1 个答案:

答案 0 :(得分:0)

看起来这个功能可以满足我的需求:

vector<wstring> ExWSQL(string command) {
    SQLHANDLE sqlConnHandle;
    SQLHANDLE sqlEnvHandle;
    SQLWCHAR retconstring[SQL_RETURN_CODE_LEN];
    sqlConnHandle = NULL;
    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &sqlEnvHandle);
    SQLSetEnvAttr(sqlEnvHandle, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
    SQLAllocHandle(SQL_HANDLE_DBC, sqlEnvHandle, &sqlConnHandle);
    SQLDriverConnect(sqlConnHandle,
        NULL,
        (SQLWCHAR*)L"DRIVER={SQL Server};SERVER=localhost, 1433;DATABASE=PhraseDB3;Trusted=true;",
        SQL_NTS,
        retconstring,
        1024,
        NULL,
        SQL_DRIVER_NOPROMPT
    );
    vector<wstring> results;
    SQLHANDLE sqlStmtHandle;
    sqlStmtHandle = NULL;
    SQLAllocHandle(SQL_HANDLE_STMT, sqlConnHandle, &sqlStmtHandle);
    wstring widestr = std::wstring(command.begin(), command.end());
    const wchar_t* temp = widestr.c_str();
    SQLExecDirectW(sqlStmtHandle, (SQLWCHAR*)temp, SQL_NTS);
    SQLWCHAR sqlRes[SQL_RESULT_LEN] = {};
    SQLINTEGER sqlResPtr;
    while (SQLFetch(sqlStmtHandle) == SQL_SUCCESS) {
        SQLGetData(sqlStmtHandle, 1, SQL_WCHAR, sqlRes, SQL_RESULT_LEN, &sqlResPtr);
        wstring result = (const WCHAR*)sqlRes;
        results.push_back(result);
    }
    SQLFreeHandle(SQL_HANDLE_STMT, sqlStmtHandle);
    SQLDisconnect(sqlConnHandle);
    SQLFreeHandle(SQL_HANDLE_DBC, sqlConnHandle);
    SQLFreeHandle(SQL_HANDLE_ENV, sqlEnvHandle);
    return results;
}

它只使用wstrings和wchars而不是字符串和字符。