ADO简单教程FOR C/C++
1. Import the type library 导入类型库
There are several ADO type libraries that can be imported, and they differ based on version. The list of type libraries available will differ based on what Windows operating system and developer tools (Visual C++ 6, Visual C++ 7.x, etc.) you have installed. In the subdirectory C:\Program Files\Common Files\System\ado on my system the list includes the following:
msado20.tlb msado21.tlb msado25.tlb msado26.tlb
For this example, I selected the latest type library file, version msado26.tlb. Then, I added the following import
statement:
#import "c:\program files\common files\system\ado\msado26.tlb" no_namespace rename( "EOF", "A_EOF" )
2. Initialize COM and create a connection object初始化 COM, 创建一个连接对象
The first steps in my example include initializing COM and creating an instance of the ADO Connection object.
HRESULT hr = ::CoInitialize(NULL);
if (FAILED(hr))
{
return false;
}
_ConnectionPtr pConnection;
hr = pConnection.CreateInstance( uuidof(Connection));
if (FAILED(hr))
{
return false;
}
3. Make the database connection 连接数据库
Now, we are ready to open a database connection. This operation is placed within a C++ try/catch block. If this operation fails, then an exception is of type _com_error is thrown and immediately caught:
pConnection->Open(strConnectionString,
_T(""), _T(""), adOpenUnspecified);
// ...
catch (_com_error&)
{
::CoUninitialize();
// ...
}
try
{
}
4. Construct a SQL statement 构造一个 SQL 语句
Finally, we are reading to execute a SQL statement. I am going to use the simple of all examples issue a
"SELECT GETDATE()" which returns a one row/column result.
_CommandPtr pCommand( uuidof(Command)); pCommand->ActiveConnection = pConnection; pCommand->CommandText = "SELECT GETDATE()";
5. Execute the statement and retrieve the results 执行该 SQL 语句并返回结果集
ADO, unlike other database abstraction layers such as JDBC, statements are executed on the result set object. To execute a statement you can create a command object and set it or link it directly to the record set object also created. The other option is to completely bypass the processes of creating command objects altogether and execute the record set immediately specifying the SQL statement text on the record set object.
_RecordsetPtr pRecordSet( uuidof(Recordset));
pRecordSet->PutRefSource(pCommand);
_variant_t vNull(DISP_E_PARAMNOTFOUND, VT_ERROR);
pRecordSet->Open(vNull, vNull, adOpenDynamic, adLockOptimistic, adCmdText);
char szTimeStamp[64] = { 0 };
if (!pRecordSet->A_EOF)
{
_Recordset **ptrResults = NULL;
pRecordSet->QueryInterface( uuidof(_Recordset), (void **) ptrResults);
The code above retrieves the resulting record set based. First however, it checks for the EOF state before it reading the rows and columns returned.
6. Iterate over the results
遍历结果集
The record set object that is returned is very simple; it only contains a single row and column of data.
// SELECT GETDATE() returns one row without a column name
_variant_t vField(_T(""));
_variant_t vResult;
vResult = pRecordSet->GetFields()->GetItem(vField)->Value;
_bstr_t strTimeStamp(vResult); strncpy(szTimeStamp, (char*) strTimeStamp, 63); if (szTimeStamp > 0)
{
char szFeedback[256] = { 0 };
sprintf(szFeedback, "SQL timestamp is: %s", szTimeStamp); AfxMessageBox(szFeedback, MB_OK | MB_ICONINFORMATION, 0);
}
7. Release resources 释放资源
pRecordSet->Close();
pConnection->Close();
::CoUninitialize();
Here we finish up by closing both the record set and the connection and releasing resources allocated for COM.
浙公网安备 33010602011771号