CRecordset field access errors
-
Hi, I'm having big trouble accessing recorset elements. I'm using ODBC(MSSQL) with CDatabase and CRecordset objects, I can perform SQL executes, and even seem to open recordsets without bother but come into trouble when I try to access a recordset field. I have CDatabase* called pDB. I have used recordsets previously but only used to check the number of rows returned (and thus not attempted to read any fields). As far as I can tell there shouldn't be a problem here (I've copied most of this section straight from MSDN). The SQL statement returns a 1 row of 1 field recordset and the datatype of that element is varchar. Hi, I've written the following code char strSQL[2048]; try { sprintf(strSQL,"SELECT DataValue FROM dbo.SSEData WHERE (DataName = N'LOGON')"); printf("%s\n\n\n", strSQL); CRecordset rs(pDB); printf("Just setup Filter\n"); if ( rs.Open( CRecordset::snapshot, strSQL) ) printf("Just opened recordset\n"); else printf("Failed to open recordset\n"); if (rs.GetRecordCount() == (long)0){ printf("No records found"); } else { if ( rs.IsEOF() ) printf("The recordset was EOF"); if ( rs.IsBOF() ) printf("The recordset was BOF"); printf("Field Count : %d \n", rs.GetODBCFieldCount() ); rs.AssertValid(); printf("Recordset was greater than nothing\n"); CString CStartupMessage; CDBVariant CVariantField; printf("In between the first and last string\n"); // rs.GetFieldValue( (short)0, CVariantField ); rs.GetFieldValue( short(0), CStartupMessage); printf("Got the field value\n"); } rs.Close(); } catch(CDBException *e){ printf("Caught a DB exception\n"); e->Delete(); } catch(CMemoryException *m){ printf("Caught a memory error\n"); TCHAR ErrorMessage[256]; m->GetErrorMessage( ErrorMessage, 255); printf("Finished capturing error %s\n", ErrorMessage); } The Output from this code is: .... Just setup Filter Just opened recordset Field Count : 1 Recordset was greater than nothing In between the first and last string Caught a memory error Can anybody help me ???? Thanks, Damian Del Campo (damian_jose@yahoo.com)
-
Hi, I'm having big trouble accessing recorset elements. I'm using ODBC(MSSQL) with CDatabase and CRecordset objects, I can perform SQL executes, and even seem to open recordsets without bother but come into trouble when I try to access a recordset field. I have CDatabase* called pDB. I have used recordsets previously but only used to check the number of rows returned (and thus not attempted to read any fields). As far as I can tell there shouldn't be a problem here (I've copied most of this section straight from MSDN). The SQL statement returns a 1 row of 1 field recordset and the datatype of that element is varchar. Hi, I've written the following code char strSQL[2048]; try { sprintf(strSQL,"SELECT DataValue FROM dbo.SSEData WHERE (DataName = N'LOGON')"); printf("%s\n\n\n", strSQL); CRecordset rs(pDB); printf("Just setup Filter\n"); if ( rs.Open( CRecordset::snapshot, strSQL) ) printf("Just opened recordset\n"); else printf("Failed to open recordset\n"); if (rs.GetRecordCount() == (long)0){ printf("No records found"); } else { if ( rs.IsEOF() ) printf("The recordset was EOF"); if ( rs.IsBOF() ) printf("The recordset was BOF"); printf("Field Count : %d \n", rs.GetODBCFieldCount() ); rs.AssertValid(); printf("Recordset was greater than nothing\n"); CString CStartupMessage; CDBVariant CVariantField; printf("In between the first and last string\n"); // rs.GetFieldValue( (short)0, CVariantField ); rs.GetFieldValue( short(0), CStartupMessage); printf("Got the field value\n"); } rs.Close(); } catch(CDBException *e){ printf("Caught a DB exception\n"); e->Delete(); } catch(CMemoryException *m){ printf("Caught a memory error\n"); TCHAR ErrorMessage[256]; m->GetErrorMessage( ErrorMessage, 255); printf("Finished capturing error %s\n", ErrorMessage); } The Output from this code is: .... Just setup Filter Just opened recordset Field Count : 1 Recordset was greater than nothing In between the first and last string Caught a memory error Can anybody help me ???? Thanks, Damian Del Campo (damian_jose@yahoo.com)
Just try rs.MoveFirst()