Using ADO in C++

Connections & Recordsets

Let’s begin by creating a connection object.

ADODB::_ConnectionPtr connection;
hr = connection.CreateInstance(__uuidof(ADODB::Connection));

Ok, that was easy. Now let’s create a recordset object.

ADODB::_RecordsetPtr recordset;
hr = recordset.CreateInstance(__uuidof(ADODB::Recordset));

Ok, this looks about as easy as Visual Basic. Somethings wrong. This can’t be that complex C++ stuff. I’m sure it’ll get harder later. Let’s try opening a connection to our database.

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

Easy again. In fact, this all looks like the equivalent Visual Basic code, but using pointers, namespaces and all those C++ thingys. Now let’s create a table using SQL.

recordset->Open("CREATE TABLE mytable (value NVARCHAR(255))",
    connection.GetInterfacePtr(), ADODB::adOpenForwardOnly,
    ADODB::adLockReadOnly, ADODB::adCmdText);

I usually don’t to DDL (Data Definition Language) from ADO, but hell it’s easy.

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.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry