MDAC 2.5 SDK - OLE DB Programmer's Reference
Chapter 4: Rowsets
The code in this example shows how to create a rowset.
/////////////////////////////////////////////////////////////////
// myCreateRowset
//
// This function creates an OLE DB rowset object from the given
// provider's session object. It first obtains a default table
// name from the user through the tables schema rowset, if
// supported, and then creates a rowset object by one of two methods:
//
// - If the user requested that the rowset object be created
// from a command object, it creates a command object and then
// obtains command text from the user, sets properties and
// the command text, and finally executes the command to
// create the rowset object.
// - Otherwise, the function obtains a table name from the user
// and calls IOpenRowset::OpenRowset to create a rowset object
// over that table that supports the requested properties.
//
/////////////////////////////////////////////////////////////////
HRESULT myCreateRowset
(
IUnknown * pUnkSession,
IUnknown ** ppUnkRowset
)
{
HRESULT hr;
IUnknown * pUnkCommand = NULL;
IOpenRowset * pIOpenRowset = NULL;
WCHAR wszTableName[MAX_NAME_LEN + 1] = {0};
const ULONG cProperties = 2;
DBPROP rgProperties[cProperties];
DBPROPSET rgPropSets[1];
// Obtain a default table name from the user by displaying the
// tables schema rowset if schema rowsets are supported.
CHECK_HR(hr = myCreateSchemaRowset(DBSCHEMA_TABLES, pUnkSession,
MAX_NAME_LEN, wszTableName));
// Set properties on the rowset, to request additional functionality.
myAddRowsetProperties(rgPropSets, cProperties, rgProperties);
// If the user requested that the rowset be created from a
// command object, create a command, set its properties and
// text, and execute it to create the rowset object.
if( g_dwFlags & USE_COMMAND )
{
WCHAR wszCommandText[MAX_NAME_LEN + 1];
// Attempt to create the command object from the provider's
// xession object. Note that commands are not supported by
// all providers, and this will fail in that case.
CHECK_HR(hr = myCreateCommand(pUnkSession, &pUnkCommand));
// Get the command text that we will execute from the user.
if( !myGetInputFromUser(wszCommandText, L"\nType the command "
L"to execute [Enter = `select * from %s`]: ", wszTableName) )
{
swprintf(wszCommandText, L"select * from %s", wszTableName);
}
// And execute the command the user entered.
CHECK_HR(hr = myExecuteCommand(pUnkCommand, wszCommandText,
1, rgPropSets, ppUnkRowset));
}
// Otherwise, the user gets the default behavior, which is to use
// IOpenRowset to create the rowset object from the session object.
// IOpenRowset is supported by all providers; it takes a TableID
// and creates a rowset containing all rows in that table. It is
// similar to using SQL command text of "SELECT * FROM TableID".
else
{
DBID TableID;
// Create the TableID.
TableID.eKind = DBKIND_NAME;
TableID.uName.pwszName = wszTableName;
// Obtain the table name from the user.
myGetInputFromUser(wszTableName, L"\nType the name of the table "
L"to use [Enter = `%s`]: ", wszTableName);
// Get the IOpenRowset interface, and create a rowset object
// over the requested table through OpenRowset.
XCHECK_HR(hr = pUnkSession->QueryInterface(
IID_IOpenRowset, (void**)&pIOpenRowset));
XCHECK_HR(hr = pIOpenRowset->OpenRowset(
NULL, // pUnkOuter
&TableID, // pTableID
NULL, // pIndexID
IID_IRowset, // riid
1, // cPropSets
rgPropSets, // rgPropSets
ppUnkRowset)); // ppRowset
}
CLEANUP:
if( pIOpenRowset )
pIOpenRowset->Release();
if( pUnkCommand )
pUnkCommand->Release();
return hr;
}