Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 69,060 times

Contents

Related Categories

Using ADO in C++ - Inserting & Selecting Records

randy

Inserting & Selecting Records

And inserting a record?

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);

Cake. In fact, I insert two records. I guess selecting records is easy too?

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();
};

Actually selecting records is bit more difficult in C++, then in Visual Basic. The code to get at the value is a nesting of pointers within pointers. This can be a little complex, but
really it’s easier than the naysayers had told us. Finally, we close the recordset and DROP the table.

recordset->Close();
recordset->Open("DROP TABLE mytable", connection.GetInterfacePtr(),
ADODB::adOpenForwardOnly, ADODB::adLockReadOnly,
ADODB::adCmdText);

That was too easy. Who said ADO in C++ was all that difficult.

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 ...