C++ ADO Connection

1k Views Asked by At

I am trying to read an excel file. I am trying to make a simple ADO connection using C++. But while opening the connection it is getting failed.

this is the code am using

#import "C:\Program Files (x86)\Common Files\System\ado\msado15.dll" rename_namespace("ADOCG") rename("EOF", "EndOfFile")

int _tmain(int argc, _TCHAR* argv[])
{
    ADOCG::_ConnectionPtr conn = NULL;
    ADOCG::_RecordsetPtr rset = NULL;

    conn.CreateInstance(__uuidof(ADOCG::Connection));
    rset.CreateInstance(__uuidof(ADOCG::Recordset));

    try
    {
        //bstr_t bstrCnxt("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\sample.mdb;Extended Properties='MSAccess 12.0;HDR=Yes'"); 
        conn->Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\weightreport.xls;Extended Properties='Excel 12.0;HDR=Yes'","","", ADOCG::adConnectUnspecified);

    }
    catch(const char * str)
    {

    }
}
1

There are 1 best solutions below

0
LawYeungKam On

You should initialize COM first. Add if (FAILED(::CoInitialize(nullptr))) {return 0;} above _ConnectionPtrand Add::CoUninitialize(); at the end of _tmain