#include <atldbcli.h>
class CTestAccessor
{
public:
LONG m_RETURNVALUE;
BEGIN_PARAM_MAP(CTestAccessor)
SET_PARAM_TYPE(DBPARAMIO_OUTPUT)
COLUMN_ENTRY(1, m_RETURNVALUE)
END_PARAM_MAP()
DEFINE_COMMAND(CTestAccessor, _T("{ ? = CALL dbo.sp_Test}"))
};
class CTestCmd : public CCommand<CAccessor<CTestAccessor> >
{
public:
HRESULT Open()
{
HRESULT hr;
hr = OpenDataSource();
if (FAILED(hr))
return hr;
return OpenRowset();
}
HRESULT OpenDataSource()
{
HRESULT hr;
CDataSource db;
CDBPropSet dbinit(DBPROPSET_DBINIT);
dbinit.AddProperty(DBPROP_AUTH_USERID, OLESTR("sa"));
dbinit.AddProperty(DBPROP_INIT_CATALOG, OLESTR("pubs"));
//change the name of SQL server to fit your environment
dbinit.AddProperty(DBPROP_INIT_DATASOURCE, OLESTR("localhost"));
dbinit.AddProperty(DBPROP_INIT_LCID, (long)1033);
dbinit.AddProperty(DBPROP_INIT_PROMPT, (short)4);
//Use this line if you want to use a forward only, read only client side cursor
hr = db.Open(_T("SQLOLEDB.1"), &dbinit);
//Use this line if you want to use an updateable client side cursor
//hr = db.OpenWithServiceComponents(_T("SQLOLEDB.1"), &dbinit);
if (FAILED(hr))
return hr;
return m_session.Open(db);
}
HRESULT OpenRowset()
{
// Set properties for open
CDBPropSet propset(DBPROPSET_ROWSET);
//Remove the following properties to prevent the error with a forward only, read only client side cursor
//Set them if you want an updateable client side cursor
propset.AddProperty(DBPROP_IRowsetChange, true);
propset.AddProperty(DBPROP_UPDATABILITY, DBPROPVAL_UP_CHANGE | DBPROPVAL_UP_INSERT | DBPROPVAL_UP_DELETE);
//Set this property if you want to use an updateable client side cursor
//propset.AddProperty(DBPROP_CLIENTCURSOR, true);
return CCommand<CAccessor<CTestAccessor> >::Open(m_session, NULL, &propset);
}
CSession m_session;
};
int main(int argc, char* argv[])
{
CoInitialize(NULL);
CTestCmd rs;
HRESULT hr=rs.Open();
if (FAILED(hr))
AtlTraceErrorRecords();
rs.Close();
return 0;
}