Dynamic accessors allow you to access a data source when you have no knowledge of the database schema (underlying structure). The OLE DB Templates library provides several classes to help you do this.
The
Using CDynamicAccessor
Example
| В | Copy Code |
|---|---|
// Using_Dynamic_Accessors.cpp
// compile with: /c /EHsc
#include <stdio.h>
#include <objbase.h>
#include <atldbcli.h>
int main( int argc, char* argv[] )
{
HRESULT hr = CoInitialize( NULL );
CDataSource ds;
CSession ss;
CTable<CDynamicAccessor> rs;
// The following is an example initialization string:
hr = ds.OpenFromInitializationString(L"Provider=SQLOLEDB.1;"
L"Integrated Security=SSPI;Persist Security Info=False;"
L"Initial Catalog=Loginname;Data Source=your_data_source;"
L"Use Procedure for Prepare=1;Auto Translate=True;"
L"Packet Size=4096;Workstation ID=LOGINNAME01;"
L"Use Encryption for Data=False;"
L"Tag with column collation when possible=False");
hr = ss.Open( ds );
hr = rs.Open( ss, "Shippers" );
hr = rs.MoveFirst( );
while( SUCCEEDED( hr ) && hr != DB_S_ENDOFROWSET )
{
for( size_t i = 1; i <= rs.GetColumnCount( ); i++ )
{
DBTYPE type;
rs.GetColumnType( i, &type );
printf_s( "Column %d [%S] is of type %d\n",
i, rs.GetColumnName( i ), type );
switch( type )
{
case DBTYPE_WSTR:
printf_s( "value is %S\n",
(WCHAR*)rs.GetValue( i ) );
break;
case DBTYPE_STR:
printf_s( "value is %s\n",
(CHAR*)rs.GetValue( i ) );
default:
printf_s( "value is %d\n",
*(long*)rs.GetValue( i ) );
}
}
hr = rs.MoveNext( );
}
rs.Close();
ss.Close();
ds.Close();
CoUninitialize();
return 0;
} | |
Using CDynamicStringAccessor
Use CDynamicStringAccessor methods to obtain column information. You use this column information to create an accessor dynamically at run time. The column information is stored in a buffer created and managed by this class. Obtain data from the buffer using
Example
| В | Copy Code |
|---|---|
// Using_Dynamic_Accessors_b.cpp
// compile with: /c /EHsc
#include <stdio.h>
#include <objbase.h>
#include <atldbcli.h>
int main( int argc, char* argv[] )
{
HRESULT hr = CoInitialize( NULL );
if (hr != S_OK)
{
exit (-1);
}
CDataSource ds;
CSession ss;
CTable<CDynamicStringAccessor> rs;
// The following is an example initialization string:
hr = ds.OpenFromInitializationString(L"Provider=SQLOLEDB.1;"
L"Integrated Security=SSPI;Persist Security Info=False;"
L"Initial Catalog=Loginname;Data Source=your_data_source;"
L"Use Procedure for Prepare=1;Auto Translate=True;"
L"Packet Size=4096;Workstation ID=LOGINNAME01;"
L"Use Encryption for Data=False;"
L"Tag with column collation when possible=False");
hr = ss.Open( ds );
hr = rs.Open( ss, "Shippers" );
hr = rs.MoveFirst( );
while( SUCCEEDED( hr ) && hr != DB_S_ENDOFROWSET )
{
for( size_t i = 1; i <= rs.GetColumnCount( ); i++ )
{
printf_s( "column %d value is %s\n",
i, rs.GetString( i ) );
}
hr = rs.MoveNext( );
}
rs.Close();
ss.Close();
ds.Close();
CoUninitialize();
return 0;
} | |
Using CDynamicParameterAccessor
The parameter information is stored in a buffer created and managed by this class. Obtain parameter data from the buffer by using
For an example demonstrating how to use this class to execute a SQLВ Server stored procedure and get the output parameter values, see Knowledge Base article Q058860, "HOWTO: Execute Stored Procedure using CDynamicParameterAccessor." Knowledge Base articles are available in the MSDN Library Visual Studio documentation or at
Dhtml editor
Free javascript download