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.