Link to home
Start Free TrialLog in
Avatar of sydneyguy
sydneyguyFlag for Australia

asked on

c++ going from Database record set into an array

have successfully retrieved the data fro the database now i need to gethis into and multi dim array so that i can operate on the information,
half way down the page is were it needs to be inserted markjed with
// *****************************************************************
// AT THIS POSITION I NEED TO GET HIS DATA INTO AN ARRAY SO THAT I CAN MANIPUPLATE THE
//  DATS AS MY NEEDS ARE DETERMINED
//*******************************************************************
i am reading up on how to do strings ext meanwhile hope you can help
thanks for reading



// This is the main project file for VC++ application project
// generated using an Application Wizard.
//
//    this works well in this confuration
//
 #include      "StdAfx.h"
//#include "stdafx.h"
#include  <atldbcli.h>
//#include  <atldbsch.h>
//#include <ostream>
#include <iostream>
#include <string>
//#include <wtypes.h>


//#using <mscorlib.dll>


//using namespace System;
using namespace std;

int _tmain()
{
    // TODO: Please replace the sample code below with your own.
    HRESULT hr = 0;
    std::string str;
    int i=0, ret=0;
   char arrayd[1][4] ;
   for(int row = 0;row<5;row++)
   {
            for(int col = 0;col<5;col++)
            {
                        cout<<arrayd[row][col]<<  "1 ";
            }
   }
 //  arrayd[4] = "kkjkjkj";
//      char array_of_strings[1][1];
//       array_of_strings[1][1] = strcpy("jjhjhjhj");
//      string DBData[5][6];
//      DBData[0][1] = "sdsdsdsd";
  //  std::string query;
      string DAM;
      //Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;User Id=admin;Password=;
      //   Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;User Id=admin;Password=;
      LPCOLESTR lpcOleConnect = L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;User Id=admin;;Password=;";

                  // To initialize the connection to a database using an OLE DB provider,
            // two ATL classes are needed: CDataSource and CSession.
            CDataSource dbDataSource;
            CSession dbSession;

            // Uses ATL's string conversion macros to convert between character encodings.
            USES_CONVERSION;

            // Open the connection and initialize the data source specified by the passed
            // initialization string.
            hr = dbDataSource.OpenFromInitializationString(lpcOleConnect);
            if (FAILED(hr))
            {
                  cout<<DAM<<": Unable to connect to data source "<<OLE2T(lpcOleConnect)<<endl;
            }
            else
            {
                  hr = dbSession.Open(dbDataSource);
                  if (FAILED(hr))
                  {
                        cout<<DAM<<": Couldn't create session on data source "<<OLE2T(lpcOleConnect)<<endl;
                  }
                  else
                  {
                        CComVariant var;
                        hr = dbDataSource.GetProperty(DBPROPSET_DATASOURCEINFO, DBPROP_DATASOURCENAME, &var);
                        if (FAILED(hr) || (var.vt == VT_EMPTY))
                        {
                              cout<<DAM<<": No Data Source Name Specified."<<endl;
                        }
                        else
                        {
                              cout<<DAM<<": Successfully connected to database. Data source name:\n  "
                                    <<COLE2T(var.bstrVal)<<endl;
x
                              // Prepare SQL query.
                              
                  //            LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName,ParentTable.a,ParentTable.b,ParentTable.c FROM ParentTable;";


                   //      LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName, ChildTable.ChildId, ChildTable.ChildName FROM ParentTable INNER JOIN ChildTable ON ParentTable.ParentId = ChildTable.ParentId;";
                              
                              LPCOLESTR query = L"SELECT * from bothtaables;";
                              
                              cout<<DAM<<": SQL query:\n  "<<OLE2T(query)<<endl;

                              // Excecute the query and create a record set.
                              CCommand<CDynamicStringAccessor> cmd;
                              hr = cmd.Open(dbSession, query);
                              DBORDINAL colCount = cmd.GetColumnCount();
                              if (SUCCEEDED(hr) && 0 < colCount)
                              {
                                    cout<<DAM<<": Retrieve schema info for the given result set: "<<endl;
                                    DBORDINAL cColumns;
                                    DBCOLUMNINFO* rgInfo = NULL;
                                    OLECHAR* pStringsBuffer = NULL;
                                    cmd.GetColumnInfo(&cColumns, &rgInfo, &pStringsBuffer);
                                    for (int col=0; col < (int)colCount; col++)
                                    {
                                          cout<<" | "<<OLE2T(rgInfo[col].pwszName);
                           
                                    }
                                    cout<<endl;

                                    cout<<DAM<<": Fetch the actual data: "<<endl;
                                    int rowCount = 0;
                                    CRowset<CDynamicStringAccessor>* pRS = (CRowset<CDynamicStringAccessor>*)&cmd;
                                    // Loop through the rows in the result set.
                                    while (pRS->MoveNext() == S_OK)
                                    {
                                          for (int col=1; col <= (int)colCount; col++)
                                          {
                                                CHAR* szValue = cmd.GetString(col);

// *****************************************************************
// AT THIS POSITION I NEED TO GET HIS DATA INTO AN ARRAY SO THAT I CAN MANIPUPLATE THE
//  DATS AS MY NEEDS ARE DETERMINED
//*******************************************************************

                                                cout<<" | "<<szValue;

                              //            DBData[0][colCount] = *szValue ;

                                          }
                                          cout<<endl;
                                          rowCount++;
                                    }
                                    cout<<DAM<<": Total Row Count: "<<rowCount<<endl;
                              }                  
                              else
                              {
                              cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
                              }
                        }  
                  }
            }

            dbDataSource.Close();
            dbSession.Close();
            cout<<DAM<<": Cleanup. Done."<<endl;

      return 0;
}
SOLUTION
Avatar of Karrtik Iyer
Karrtik Iyer
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sydneyguy

ASKER

thanks for all your help will revisit this area in a week when i come to stream lining this area again thanks for your help