Sort Property Example (VC++)
This example uses the Recordset object's Sort property to reorder the rows of a Recordset derived from the Authors table of the Pubs database. A secondary utility routine prints each row.
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
#include <ole2.h>
#include <stdio.h>
#include <conio.h>
// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void SortX(void);
void SortXprint(_bstr_t title, _RecordsetPtr rstp);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);
//////////////////////////////////////////////////////////
// //
// Main Function //
// //
//////////////////////////////////////////////////////////
void main()
{
if(FAILED(::CoInitialize(NULL)))
return;
SortX();
::CoUninitialize();
}
//////////////////////////////////////////////////////////
// //
// SortX Function //
// //
//////////////////////////////////////////////////////////
void SortX(void)
{
HRESULT hr = S_OK;
// Initialize pointers on define.
// These are in the ADODB:: namespace.
_ConnectionPtr pConnection = NULL;
_RecordsetPtr pRstAuthors = NULL;
// Define string variables.
_bstr_t strCnn("Provider=sqloledb;Data Source=srv;"
"Initial Catalog=Pubs;User Id=sa;Password=;");
try
{
TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
TESTHR(pRstAuthors.CreateInstance(__uuidof(Recordset)));
pRstAuthors->CursorLocation = adUseClient;
pConnection->Open (strCnn, "", "", NULL);
pRstAuthors->Open("SELECT * FROM Authors",
_variant_t((IDispatch *) pConnection),
adOpenStatic, adLockReadOnly, adCmdText);
SortXprint(" Initial Order ", pRstAuthors);
//Clear the screen for the next display.
printf("\nPress any key to continue...");
getch();
system("cls");
pRstAuthors->Sort = "au_lname ASC, au_fname ASC";
SortXprint("Last Name Ascending", pRstAuthors);
//Clear the screen for the next display.
printf("\nPress any key to continue...");
getch();
system("cls");
pRstAuthors->Sort = "au_lname DESC, au_fname ASC";
SortXprint("Last Name Descending", pRstAuthors);
// Clean up objects before exit.
pRstAuthors->Close();
pConnection->Close();
}
catch(_com_error &e)
{
PrintProviderError(pConnection);
PrintComError(e);
}
}
//////////////////////////////////////////////////////////
// //
// SortXprint Function //
// //
//////////////////////////////////////////////////////////
//This is the secondary utility routine that prints
//the given title, and the contents of the specified Recordset.
void SortXprint(_bstr_t title, _RecordsetPtr rstp)
{
printf("---------------%s---------------\n", (LPCSTR)title);
printf("First Name Last Name\n"
"---------------------------------------------------\n");
rstp->MoveFirst();
int intLineCnt = 4;
while (!(rstp->EndOfFile))
{
_bstr_t aufname;
_bstr_t aulname;
aufname = rstp->GetFields()->GetItem("au_fname")->Value;
aulname = rstp->GetFields()->GetItem("au_lname")->Value,
printf("%s %s\n",(LPCSTR) aufname, (LPCSTR) aulname);
rstp->MoveNext();
intLineCnt++;
if (intLineCnt % 20 ==0)
{
printf("\nPress any key to continue...\n");
getch();
}
}
}
//////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
//////////////////////////////////////////////////////////
void PrintProviderError(_ConnectionPtr pConnection)
{
// Print Provider Errors from Connection object.
// pErr is a record object in the Connection's Errors collection.
ErrorPtr pErr = NULL;
if( (pConnection->Errors->Count) > 0)
{
long nCount = pConnection->Errors->Count;
// Collection ranges from 0 to nCount -1.
for(long i = 0; i < nCount; i++)
{
pErr = pConnection->Errors->GetItem(i);
printf("Error number: %x\t%s\n", pErr->Number,
(LPCSTR) pErr->Description);
}
}
}
//////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
//////////////////////////////////////////////////////////
void PrintComError(_com_error &e)
{
_bstr_t bstrSource(e.Source());
_bstr_t bstrDescription(e.Description());
// Print Com errors.
printf("Error\n");
printf("\tCode = %08lx\n", e.Error());
printf("\tCode meaning = %s\n", e.ErrorMessage());
printf("\tSource = %s\n", (LPCSTR) bstrSource);
printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}