Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 68,795 times

Contents

Related Categories

Using ADO in C++ - Full Listing

randy

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";
    };
}

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

Comments

  • Posted by souvik on 29 Aug 2005


    yes , it can be used, but in this case , compiler will treat it as auto variable. Compiler will only generate warning.

  • register variable

    Posted by carthyc on 15 Jul 2005

    hai,

    can we use register variable for storing address

  • I now interested in this,hope any news about this:

    Posted by angus on 10 Jul 2005

    :o :(

  • C language and ADO usage

    Posted by rassul on 02 May 2005

    Hi,

    Does anyone know how to use C language (not C++) with ADO and/or ODBC againt SQL Server. Any sample or a link to a sample is very much appreciated. Thanks in advance.

    Rass

  • Adding a Record

    Posted by vohalloran on 24 Apr 2005

    Now show me how to add a new record to the database. After many hours of working on this, I finally got the program to retrieve the records from the Access database, but I have no idea how to add a ...