HRESULT CDBStepNC::SelectTable(CString tszSQL, StringTable &szColumnTable, StringTable &szDataTable) { USES_CONVERSION; HRESULT hr; //CTable<CDynamicAccessor> rs; CTable<CDynamicStringAccessor> rs; hr = rs.Open( m_session, "milling_cutter" ); hr = rs.MoveFirst( ); int j=0; while( SUCCEEDED( hr ) && hr != DB_S_ENDOFROWSET ) { for( size_t i = 1; i < rs.GetColumnCount( ); i++ ) { OutputDebugString(StrFormat( "Column %d [%S]= %s\n", i, rs.GetColumnName( i ), rs.GetString( i ) )); if(j==0) szColumnTable(0,i)=W2T(_bstr_t(rs.GetColumnName( i ))); szDataTable(j,i-1)=rs.GetString( i ); OutputDebugString(szDataTable(j,i)); } hr = rs.MoveNext( ); j++; } return hr; }
void CQueryWizard::ExecuteQuery( CQueryBuilder& builder ) { //MessageBox( 0, builder.GetQuery().c_str(), "query", 0 ); CConnection conn; if ( !conn.OpenForReading() ) { MessageBox( 0, "Unable to connect to the database!", "", 0 ); return; } try { BeginWaitCursor(); CTable rs = conn.Execute( builder.GetQuery() ); char tempPath[4001]; char tempFile[4001]; ::GetTempPath( 4000, tempPath ); int fileNum = 0; FILE* f = NULL; while ( !f ) { sprintf( tempFile, "%s\\studentinfo%d.xls", tempPath, fileNum++ ); f = fopen( tempFile, "w" ); if ( !f && fileNum == 0 ) { MessageBox( 0, "Unable to create file to store query!", "StudentInfo QueryWizard", 0 ); return; } } vector<string> header = builder.GetSelect(); for ( int j=0; j<header.size(); j++ ) { if ( j ) { fprintf( f, "\t%s", header[j].c_str() ); } else { fprintf( f, "%s", header[j].c_str() ); } } fprintf( f, "\n" ); while ( !rs.IsAtEOF() ) { for ( int i=0; i<rs.GetColumnCount(); i++ ) { if ( i ) { fprintf( f, "\t%s", ((string)rs[i]).c_str() ); } else { fprintf( f, "%s", ((string)rs[i]).c_str() ); } } fprintf( f, "\n" ); rs.MoveNext(); } fclose(f); ShellExecute( NULL, "open", tempFile, NULL, NULL, SW_SHOWNORMAL ); EndWaitCursor(); } catch ( ... ) { //char temp[10000]; //if ( e.GetErrorMessage( temp, 10000 ) ) { MessageBox( 0, "Unable to execute the query!", "", 0 ); //} else { // MessageBox( 0, ( (string)"Unable to execute the query: " + (string)temp ).c_str(), "", 0 ); //} } }