Skip to Content.
Sympa Menu

freetds - Re: [freetds] Stange ODBC behaviour

freetds AT lists.ibiblio.org

Subject: FreeTDS Development Group

List archive

Chronological Thread  
  • From: Craig Davison <cd AT securityfocus.com>
  • To: freetds AT lists.ibiblio.org
  • Subject: Re: [freetds] Stange ODBC behaviour
  • Date: Wed, 12 Feb 2003 16:37:28 -0700

Sorry to reply to myself 3 times :)

Here's a sample program that illustrates the problem. To try, create a
sp_return_nothing stored proc that returns no rowset, or create the one
defined in the comments. You'll also need to change the DSN, user and
password in main().

--
Craig Davison
Symantec Corporation
+1 (403) 213-3939 ext. 228
/* Create the stored proc as follows:
*
* CREATE PROCEDURE dbo.sp_return_nothing
* AS BEGIN
* SELECT TOP 1 uid INTO #nothing FROM master..sysusers
* END
* GO
*/



#include <stdio.h>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>

int CheckSQLHandle (SQLSMALLINT SQLHandleType, SQLHSTMT hStmt, long erg,
const char *pDesc)
{
SQLINTEGER SQLerr;
SQLSMALLINT SQLmsglen;
char pStatus[10], pMsg[501];
long erg2;

if (erg != SQL_SUCCESS)
{
fprintf(stderr, "Error %s %ld\n", pDesc, erg);
erg2 = SQLGetDiagRec(SQLHandleType, hStmt,1,
pStatus,&SQLerr,pMsg,500,&SQLmsglen);
if (erg2 == SQL_SUCCESS)
{
fprintf(stderr, "%s (%d)\n",pMsg,(int)SQLerr);
}
else if (erg2 == SQL_SUCCESS_WITH_INFO)
{
fprintf(stderr, "Error buffer not large enough!\n");
}
return -1;
}
return 0;
}

void TryQuery (SQLHDBC hOdbc)
{
SQLINTEGER SQLid;
SQLINTEGER SQLerr;
long erg;
SQLHSTMT hStmt;
char pQuery[501];

SQLAllocHandle(SQL_HANDLE_STMT, hOdbc, &hStmt);
sprintf (pQuery, "sp_return_nothing");
printf ("First query: %s\n", pQuery);
erg = SQLExecDirect (hStmt, pQuery, SQL_NTS);
if (CheckSQLHandle (SQL_HANDLE_STMT, hStmt, erg, "First query"))
{
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);
return;
}
SQLFreeStmt (hStmt, SQL_CLOSE);
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);

SQLAllocHandle(SQL_HANDLE_STMT, hOdbc, &hStmt);
sprintf (pQuery, "SELECT uid FROM master..sysusers");
printf ("Second query: %s\n", pQuery);
SQLBindCol(hStmt, 1, SQL_C_SLONG, &SQLid, sizeof(SQLINTEGER),
&SQLerr);
erg = SQLExecDirect (hStmt, pQuery, SQL_NTS);
if (CheckSQLHandle (SQL_HANDLE_STMT, hStmt, erg, "Second query"))
{
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);
return;
}
erg = SQLFetch (hStmt);
while (erg != SQL_NO_DATA && erg != SQL_ERROR)
{
printf ("Data: %d\n", (int)SQLid);
}
if (erg == SQL_ERROR && CheckSQLHandle (SQL_HANDLE_STMT, hStmt, erg,
"SQLFetch Second query"))
{
SQLFreeStmt (hStmt, SQL_CLOSE);
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);
return;
}
SQLFreeStmt (hStmt, SQL_CLOSE);
SQLFreeHandle(SQL_HANDLE_STMT, &hStmt);
}

int main (int argc, char **argv)
{
SQLHENV hEnv;
SQLHDBC hOdbc;
long erg;

SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &hEnv);
SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
SQLAllocHandle(SQL_HANDLE_DBC, hEnv, &hOdbc);

/* connect to the database */
erg = SQLConnect(hOdbc, (SQLCHAR*) "mydb", SQL_NTS,
(SQLCHAR*) "user", SQL_NTS,
(SQLCHAR*) "pass", SQL_NTS);
if (CheckSQLHandle (SQL_HANDLE_DBC, hOdbc, erg, "Database Connect"))
{
SQLFreeHandle(SQL_HANDLE_DBC, &hOdbc);
SQLFreeHandle(SQL_HANDLE_ENV, &hEnv);
return 0;
}

TryQuery (hOdbc);

SQLDisconnect(hOdbc);
SQLFreeHandle(SQL_HANDLE_DBC, &hOdbc);
SQLFreeHandle(SQL_HANDLE_ENV, &hEnv);

return 0;
}




Archive powered by MHonArc 2.6.24.

Top of Page