Using ADO in C++

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.

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.

“C++: an octopus made by nailing extra legs onto a dog.” - Steve Taylor