c++ OLEDB connection error to msaccess from c++

 typedef /* [string] */ const OLECHAR *LPCOLESTR;
SO THT propblem has been resolved

other errors
------ Build started: Project: accessMSdb, Configuration: Debug Win32 ------


Build log was saved at "file://c:\c++ tree view\new files\textfile\accessMSdb\Debug\BuildLog.htm"
accessMSdb - 5 error(s), 0 warning(s)

---------------------- Done ----------------------

    Build: 0 succeeded, 1 failed, 0 skipped

c:\c++ tree view\new files\textfile\accessMSdb\accessMSdb.cpp(28): error C2440: 'initializing' : cannot convert from 'const unsigned short [114]' to 'LPCOLESTR'
c:\c++ tree view\new files\textfile\accessMSdb\accessMSdb.cpp(40): error C2664: 'ATL::CDataSource::OpenFromInitializationString' : cannot convert parameter 1 from 'LPCOLESTR' to 'LPCOLESTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\c++ tree view\new files\textfile\accessMSdb\accessMSdb.cpp(66): error C2040: 'query' : 'LPCOLESTR' differs in levels of indirection from 'std::string'
c:\c++ tree view\new files\textfile\accessMSdb\accessMSdb.cpp(66): error C2440: 'initializing' : cannot convert from 'const unsigned short [67]' to 'LPCOLESTR'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\c++ tree view\new files\textfile\accessMSdb\accessMSdb.cpp(71): error C3861: 'query': identifier not found, even with argument-dependent lookup

// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include "stdafx.h"
#include  <atldbcli.h>
#include  <atldbsch.h>
#include <ostream>
#include <iostream>
#include <string>

#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;
    std::string query;
      string DAM;
      LPCOLESTR lpcOleConnect = L"Provider=Microsoft.ACE.OLEDB.12.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.

            // 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;
                              // Prepare SQL query.
                              LPCOLESTR query = L"SELECT Customers.[Company], Customers.[First Name] FROM Customers;";
//                              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<<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);
                                                cout<<" | "<<szValue;
//                                    cout<<DAM<<": Total Row Count: "<<rowCount<<endl;
//                                    cout<<DAM<<": Error: Number of fields in the result set is 0."<<endl;
//                        }  
//                  }
      //      }

//            cout<<DAM<<": Cleanup. Done."<<endl;

      return 0;
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cannot convert from 'const unsigned short [114]' to 'LPCOLESTR'
LPCOLESTR is a pointer to wchar_t type. wchar_t is an own type which has the same size as short (for most compilers) but cannot implicitly convert from short to wchar_t.

so you have to use either an array of wchar_t or do an explicit cast.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

you need to remove the (wrong) typedef and include wtypes.h .

note, stdafx.h normally includes wtypes.h (regardless whether you were using precompiled headers or not).

if you remove the typedef the compiler should find LPCOLESTR. try f12 on LPCOLESTR to find the definition.

Protecting & Securing Your Critical Data

Considering 93 percent of companies file for bankruptcy within 12 months of a disaster that blocked access to their data for 10 days or more, planning for the worst is just smart business. Learn how Acronis Backup integrates security at every stage

is your project set to multibyte character set or unicode (utf-16)? look at the general page in th eproject properties.

sydneyguyAuthor Commented:
yes you were right fixed this up and now have access the db recordset.
you might be able to tell me i am now cycling through the data set and saving it to the console and it works just fine,
so to manipulte the data i was going to drop it into an array.
i can do it in a single element array, and can do it in a multi but when i look at it in the debugger under locals, the multi does not show the array as i woudl have expected, any idea whey
thanks for you help
SINGLE -----------------------
int _tmain()

      char arrayd[2] = {1,2};
      for(int row = 0;row<5;row++)
      return 0;

MULTI -----------------------------------
int _tmain()

      char arrayd[2][2] ={ {1,2},{7,8}};
      for(int row = 0;row<2;row++)
            for(int col = 0;col<2;col++)
      return 0;
sydneyguyAuthor Commented:
it works fine and shows ok as expected in vs2008 express
sydneyguyAuthor Commented:
thanks for you help seems its a prob with vs2003 so will just move past it
sydneyguyAuthor Commented:
thanks for all your help
   its a prob with vs2003 so will just move past it

yes. the debugger in visual studio had some problems with arrays in the past.

generally c and c++ cannot handle fixed sized arrays properly. the problem is that the information of the dimensions is only available within the scope you created the array. latest, if you pass an array to a function, the variable turned to a pointer and unless you also passed the dimensions with the array, this information is lost and the called function needs to "know" the dimensions. the sizeof function would return pointer size what is of no value. for a multidimensional array things go worse, actually such an array internally only has 1 dimension. for a 2d array you may imagine all "rows" to be concatenated. because of that you could "cast" an array [2][6] to an array [3][4], or [4][3], or [6][2], or even an 1d-array [12] without issues as long as you calculate "row" and "column" index properly.

note, to come out of the problems you should consider to using std::vector instead of fixed c arrays. your sample code would turn to

// a 2d-array is a vector of vectors
std::vector<std::vector<char> > array2d(2, std::vector<char>(2));

array2d[0][0] = 1;
array2d[0][1] = 2;
array2d[1][0] = 3;
array2d[1][1] = 4;

for (int row = 0; row < 2; ++row)
     for (int col = 0; col < 2; ++col)
         std::cout << (int)array2d[row][col] << ' ';

Open in new window

while the initialisation of such a vector is not very comfortable (what doesn't make so much an issue if you load data from file or database anyhow), you see, that the access to the 2d array is absolutely identically. moreover, you could dynamically change the dimensions and sizes what makes this container much simpler useable than it is with fixed-sized arrays.

also the debugger would/should show the variables much better, though i don't remember whether  vs2003 had some lacks or not.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.