Problem getting list of fields in table 
Author Message
 Problem getting list of fields in table

I'd appreciate it if someone could shed some light on a problem I'm having
getting a list of the fields in a table as I can't get it working and it's
driving me nuts!

Basically it works for a table I import from another database, but if I
create a new table and add some fields to it, it says there are no fields in
the database. Here's the code I'm using, here's the class for getting field
lists, it gets passed on construction a reference to a Connection object,
which handles the connection to the database:

class FieldList
{
public:
 FieldList( Connection& conn, LPCTSTR pszTableName );
 ~FieldList();

 bool MoveNext() { return SQLFetch( m_hstmt ) != SQL_NO_DATA; }
 LPCTSTR GetName() { return m_szFieldName; }

private:
 TableList();

 Connection& m_conn;
 SQLHSTMT m_hstmt;
 TCHAR m_szFieldName[ 100 ];
 SQLINTEGER m_cbFieldName;

Quote:
};

FieldList::FieldList( Connection& conn, LPCTSTR pszTableName )
 : m_conn( conn )
 , m_hstmt( SQL_NULL_HSTMT )
{
 // allocate a statement handle.
 if( SQL_FAILED( SQLAllocHandle( SQL_HANDLE_STMT, conn.GetConnection(),
&m_hstmt ) ) )
  throw new Exception( _T("Unable to allocate a statement handle.") );

 // get a list of the fields.
 if( SQL_FAILED( SQLColumns( m_hstmt, NULL, 0, NULL, 0,
(SQLCHAR*)pszTableName, SQL_NTS, NULL, 0 ) ) )
  throw new Exception( _T("Unable to get field list.") );

 m_szFieldName[ 0 ] = _T('\0');

 if( SQL_FAILED( SQLBindCol( m_hstmt, 4, SQL_C_CHAR, m_szFieldName,
countof( m_szFieldName ), &m_cbFieldName ) ) )
  throw new Exception( _T("Unable to bind column.") );

Quote:
}

FieldList::~FieldList()
{
 // free the statement handle.
 if( m_hstmt != SQL_NULL_HSTMT )
  if( SQL_FAILED( SQLFreeHandle( SQL_HANDLE_STMT, m_hstmt ) ) )
   throw new Exception( _T("Unable to free the statement handle.") );

Quote:
}

And here's the code using this to get the list of fields:

 try
 {
  // already got Connection object conn.
  FieldList list( conn, _T("MyTable") );

  while( list.MoveNext() )
   VERIFY( m_ctlSourceFields.AddString( list.GetName() ) != LB_ERR );
 }
 catch( Exception* pe )
 {
  pe->ReportError();
  pe->Delete();
 }

I tried changing Fetch to FetchScroll but this made no difference.

Thanks for any help,
Mark



Tue, 07 Sep 2004 20:16:51 GMT
 
 [ 1 post ] 

 Relevant Pages 

1. Getting a list of fields in a table...

2. Getting a list of Tables and fields in an access database

3. getting list of fields from recordset

4. Getting a list of items on a report with summary fields

5. getting oracle tables list

6. Getting a list of Tables using Borland C++ Builder 5/MS SQL Server 8

7. Getting a list of all replicated tables?

8. Getting a list of tables in a database with Perl

9. Getting list of columns in a table

10. Getting a list of tables and columns

11. getting a list of table names

12. Getting a list of tables from a Access DB


 
Powered by phpBB® Forum Software