Using ADO in C++

Full Listing

The full listing of code wrapped into a console application follows.

#include <iostream>
#include <string>
#import "C:\Program files\Common Files\System\Ado\msado15.dll"
rename("EOF", "ADOEOF")
std::string outputashex(unsigned long l)
{
    char buffer[1024];
    ::itoa(l, buffer, 16);
    return buffer;
} ;
void main()
{
    HRESULT hr;
    CoInitialize(NULL);
    try
    {
        ADODB::_ConnectionPtr connection;
        hr = connection.CreateInstance(__uuidof(ADODB::Connection));
        if (FAILED(hr))
        {
            throw _com_error(hr);
        }
        ADODB::_RecordsetPtr recordset;
        hr = recordset.CreateInstance(__uuidof(ADODB::Recordset));
        if (FAILED(hr))
        {
            throw _com_error(hr);
        }
        connection->CursorLocation = ADODB::adUseClient;
        connection->Open(L"Provider=sqloledb;Data Source=fifa;"
        L"Initial Catalog=test;User Id=testsa;Password=testsa;", L"",
        L"", ADODB::adConnectUnspecified);
        recordset->Open("CREATE TABLE mytable (value NVARCHAR(255))",
        connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
        ADODB::adLockReadOnly, ADODB::adCmdText);
        recordset->Open("INSERT INTO mytable VALUES ('Hello')",
        connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
        ADODB::adLockReadOnly, ADODB::adCmdText);
        recordset->Open("INSERT INTO mytable VALUES ('Goodbye')",
        connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
        ADODB::adLockReadOnly, ADODB::adCmdText);
        recordset->Open("SELECT * from mytable",
        connection.GetInterfacePtr(),
        ADODB::adOpenForwardOnly, ADODB::adLockReadOnly,
        ADODB::adCmdText);
        while(!recordset->ADOEOF)
        {
            _variant_t var;
            var = recordset->Fields->GetItem(L"value")->GetValue();
            std::cout << static_cast<char *>(_bstr_t(var.bstrVal))
            << std::endl;
            recordset->MoveNext();
        };
        recordset->Close();
        recordset->Open("DROP TABLE mytable", connection.GetInterfacePtr(),
        ADODB::adOpenForwardOnly, ADODB::adLockReadOnly,
        ADODB::adCmdText);
    }
    catch(_com_error &e)
    {
        std::cerr << ::outputashex(hr) << ":"
        << static_cast<char *>(e.Description());
    }
    catch(...)
    {
        std::cerr << "Unhandled Exception";
    };
}

You might also like...

Comments

About the author

Randy Charles Morin

Randy Charles Morin Canada

Randy's article are Copyright 1998-2003 Randy Charles Morin

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There are only two kinds of languages: the ones people complain about and the ones nobody uses” - Bjarne Stroustrup