Issue with table parameter types and varchar column in C++ ODBC

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi,

I am experimenting with 'table' user data types in SQL Server 2008 sp1 and I
am getting a consistent error from the connection when I pass 'character
arrays' across the interface (ints/floats appear to work fine) to a stored
procedure with a table parameter type as an argument. The error is:

"Execute Failed: [Microsoft][SQL Server Native Client 10.0]Unspecified error
occurred on SQL Server. Connection may have been terminated by the server."

It occurs on the 'second' SP call to the interface using the same connection
handle. The first loop SP execution does succeed and character arrays
specified are inserted into the SQL server table from the table parameter
type.

An abbreviated C++ code sample is below. I have been careful to remove all
state on each loop apart from the connection, but the error still occurs,
perhaps indicating the connection handle is not removing internal state
correctly. Also the C++ character string array handling is tricky, perhaps
there is a cleaner implementation available.

Any help appreciated,

Barnaby



SQLHDBC dbc = NULL;
SQLRETURN retcode = NULL;

retcode = SQLDriverConnect(dbc, NULL, (SQLCHAR*)"Driver={SQL Server Native
Client 10.0};Server=(local);Trusted_Connection=Yes;Database=testdb;",
SQL_NTS,NULL, 0, NULL, SQL_DRIVER_COMPLETE);

if ( !SQL_SUCCEEDED(retcode) ) // Fail
{
return;
}

// Loop 10 times, inserting 10 rows each time
for (int nLoops = 0; nLoops < 10; i++)
{

SQLHSTMT hstmt = NULL;
// Allocate statement handle
SQLRETURN retcode = SQLAllocHandle(SQL_HANDLE_STMT, dbc, &hstmt);

if ( !SQL_SUCCEEDED(retcode) ) // Fail
{
break;
}

#define SQL_VARCHAR_SIZE 8
SQLULEN nInsertRows = 10;
SQLCHAR *szCharBuffer = new SQLCHAR[SQL_VARCHAR_SIZE*nInsertRows];
SQLLEN *pLenBuffer = new SQLLEN[nInsertRows];
::ZeroMemory(szCharBuffer, nInsertRows*SQL_VARCHAR_SIZE);
SQLLEN cbTVP = 0;
retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_DEFAULT,
SQL_SS_TABLE, nInsertRows, 0, (SQLPOINTER)1, 0, &cbTVP);

// Bind columns for the table-valued parameter
// First set focus on param 1
retcode = SQLSetStmtAttr(hstmt, SQL_SOPT_SS_PARAM_FOCUS, (SQLPOINTER) 1,
SQL_IS_INTEGER);
// VarChar Table Parameter
retcode = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR ,
SQL_VARCHAR, SQL_VARCHAR_SIZE-1, 0, szCharBuffer, SQL_VARCHAR_SIZE,
pLenBuffer);
// Reset param focus.
retcode = SQLSetStmtAttr(hstmt, SQL_SOPT_SS_PARAM_FOCUS, (SQLPOINTER) 0,
SQL_IS_INTEGER);

// Number of rows available for input.
::ZeroMemory(szCharBuffer, nInsertRows*SQL_VARCHAR_SIZE); // Clear
cbTVP = 0;
for (UINT nPos = 0; nPos < nInsertRows; nPos++)
{
sprintf((char*)(szCharBuffer+(cbTVP*SQL_VARCHAR_SIZE)), "Test"); // Set
each 8 character array to 'Test'
pLenBuffer[cbTVP] = SQL_NTS;
cbTVP++;
}

// Call the procedure.
retcode = SQLExecDirect(hstmt, (SQLCHAR *) "{call INSERT_TEST(?)}",SQL_NTS);

if ( !SQL_SUCCEEDED(retcode) ) // Fail
{
ReportError(hstmt);
}

// Cleanup
delete szCharBuffer;
delete pLenBuffer;
SQLFreeHandle(SQL_HANDLE_STMT, hstmt);

}
.



Relevant Pages

  • Re: couple c and fortran 95
    ... I would always put middleware layer at either side of the interface not just ... Fortran by Reference. ... arrays or by passing a pointer from the C side of the interface. ... C strings are null terminated arrays of character; ...
    (comp.lang.fortran)
  • Re: Help !!! Unable to connecte Asus 620
    ... inherit the connection problem. ... >> socket connection. ... >>>serial pin is low, possible cable problem. ...
    (microsoft.public.pocketpc.activesync)
  • Re: Last Ninja Programming Language
    ... So when 88 unique variables just aren't enough (not including arrays). ... than just two character variable names. ... ``GOSUB MoveEnemy`` is much clearer than ``GOSUB 2770`` and ... labels that "compiled" such a program into compact BASIC V2. ...
    (comp.sys.cbm)
  • Re: Access one character in an array of characters
    ... > points but also display the cards of a particulier hand by suit. ... > To print out the values by rank I have used this while loop sequence: ... > the loop works it would display any character that is not the null ... raw arrays is not the 'OO way'. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Titan Quest is the Next BIG THING!
    ... I can change the DNS info but it's just not useable by the existing ... bounced out within 5 minutes - low server load, smooth connection. ... the starting character is just a base of attributes - it changes ... tech from any race - your race only determines ...
    (comp.sys.ibm.pc.games.rpg)