SQLBindParameter and SQLExecute returns SQL_NEED_DATA

我只是一个虾纸丫 提交于 2021-02-20 19:07:53

问题


I'm working on a C application that interacts with a mssql database running on Windows 2008 R2. I'm able to connect to the database and run specific queries, but when i use SQLBindParameter things fall apart.

I found a post on stackoverflow that appears to be the same problem, but the solution doesnt appear to be the same (Problems getting SQLBindParameter to work in C++). According to C Data types SQL_C_CHAR is correct (https://msdn.microsoft.com/en-us/library/windows/desktop/ms714556(v=vs.85).aspx)

Here is the code

    SQLINTEGER strlentemp = SQL_NTS;
    SQLCHAR *sqlstmt = "select * from detail where name = ?";
    status = SQLPrepare(hstmt->hstmt, sqlstmt, SQL_NTS);
    unsigned char *temp = "myvalue";
    status = SQLBindParameter(hstmt->hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, 50,0,temp,strlen(temp),&strlentemp);
    status = SQLExecute(hstmt->hstmt);

SQLExecute will return 99, SQL_NEED_DATA. If i call SQLParamData after SQLExecute it also returns 99 and the ValuePtrPtr (the 2nd argument) is the data i've passed through (temp).

Any thoughts? What am i missing?


回答1:


If SQLBindParameter() succeeds but SQLExecute() returns SQL_NEED_DATA then you have an incorrect usage of SQLBindParameter(), usually either the value size or field size.

Try the following code for ANSI and wide character strings:

SQLRETURN SQLBindStringW(SQLHSTMT hstmt, SQLUSMALLINT ipar, LPCWSTR szString)
{
    SQLRETURN ret;
    SQLULEN len;
    SQLINTEGER size;
    SQLPOINTER pData;

    len = wcslen(szString);
    size = len * sizeof(WCHAR);
    pData = (SQLPOINTER)szString;
    StrLen = SQL_NTSL;
    ret = ::SQLBindParameter(hstmt, ipar, SQL_PARAM_INPUT, SQL_C_WCHAR, SQL_WVARCHAR, len, 0, pData, size, &StrLen);
    return ret;
}

SQLRETURN SQLBindStringA(SQLHSTMT hstmt, SQLUSMALLINT ipar, LPCSTR szString)
{
    SQLRETURN ret;
    SQLULEN len;
    SQLINTEGER size;
    SQLPOINTER pData;

    len = strlen(szString);
    size = len * sizeof(CHAR);
    pData = (SQLPOINTER)szString;
    StrLen = SQL_NTSL;
    ret = ::SQLBindParameter(hstmt, ipar, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, len, 0, pData, size, &StrLen);
    return ret;
}


来源:https://stackoverflow.com/questions/32655466/sqlbindparameter-and-sqlexecute-returns-sql-need-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!