Rowset Creation Example
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, 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, 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
// Session 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) )
{
if (_snwprintf_s(wszCommandText, sizeof(wszCommandText ), MAX_NAME_LEN,
L"select * from %s", wszTableName) < 0)
return S_FALSE;
else
wszCommandText[MAX_NAME_LEN] = L'\0';
}
// 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;
}