sydneyguy
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.O LEDB.4.0;D ata Source=C:\mydatabase.mdb;U ser Id=admin;Password=;
// Provider=Microsoft.Jet.OLE DB.4.0;Dat a Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;Us er Id=admin;Password=;
LPCOLESTR lpcOleConnect = L"Provider=Microsoft.Jet.O LEDB.4.0;D ata Source=C:\\c++ tree view\\new files\\DB\\TreeView.mdb;Us er 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.OpenFromIniti alizationS tring(lpcO leConnect) ;
if (FAILED(hr))
{
cout<<DAM<<": Unable to connect to data source "<<OLE2T(lpcOleConnect)<<e ndl;
}
else
{
hr = dbSession.Open(dbDataSourc e);
if (FAILED(hr))
{
cout<<DAM<<": Couldn't create session on data source "<<OLE2T(lpcOleConnect)<<e ndl;
}
else
{
CComVariant var;
hr = dbDataSource.GetProperty(D BPROPSET_D ATASOURCEI NFO, 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)<<end l;
x
// Prepare SQL query.
// LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName,Par entTable.a ,ParentTab le.b,Paren tTable.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<CDynamicStringAcc essor> 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(&cColumn s, &rgInfo, &pStringsBuffer);
for (int col=0; col < (int)colCount; col++)
{
cout<<" | "<<OLE2T(rgInfo[col].pwszN ame);
}
cout<<endl;
cout<<DAM<<": Fetch the actual data: "<<endl;
int rowCount = 0;
CRowset<CDynamicStringAcce ssor>* pRS = (CRowset<CDynamicStringAcc essor>*)&c md;
// 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;
}
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.O
// Provider=Microsoft.Jet.OLE
LPCOLESTR lpcOleConnect = L"Provider=Microsoft.Jet.O
// 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.OpenFromIniti
if (FAILED(hr))
{
cout<<DAM<<": Unable to connect to data source "<<OLE2T(lpcOleConnect)<<e
}
else
{
hr = dbSession.Open(dbDataSourc
if (FAILED(hr))
{
cout<<DAM<<": Couldn't create session on data source "<<OLE2T(lpcOleConnect)<<e
}
else
{
CComVariant var;
hr = dbDataSource.GetProperty(D
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)<<end
x
// Prepare SQL query.
// LPCOLESTR query = L"SELECT ParentTable.ParentId, ParentTable.ParentName,Par
// 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<CDynamicStringAcc
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(&cColumn
for (int col=0; col < (int)colCount; col++)
{
cout<<" | "<<OLE2T(rgInfo[col].pwszN
}
cout<<endl;
cout<<DAM<<": Fetch the actual data: "<<endl;
int rowCount = 0;
CRowset<CDynamicStringAcce
// 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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER