IAccessor
От: RZvarych Украина  
Дата: 29.09.04 18:52
Оценка:
int main(int argc, char* argv[])
{
IUnknown * pIUnknown = NULL;
BOOL * pbValue = FALSE;
CLSID clsid;
HRESULT hr;

typedef _com_ptr_t< _com_IIID<IDataInitialize, &__uuidof(IDataInitialize)> > IDataInitializePtr;
typedef _com_ptr_t< _com_IIID<IDBInitialize, &__uuidof(IDBInitialize)> > IDBInitializePtr;
typedef _com_ptr_t< _com_IIID<IDBProperties, &__uuidof(IDBProperties)> > IDBPropertiesPtr;
typedef _com_ptr_t< _com_IIID<IDBCreateSession, &__uuidof(IDBCreateSession)> > IDBCreateSessionPtr;
typedef _com_ptr_t< _com_IIID<IDBCreateCommand, &__uuidof(IDBCreateCommand)> > IDBCreateCommandPtr;
typedef _com_ptr_t< _com_IIID<IRowset, &__uuidof(IRowset)> > IRowsetPtr;
typedef _com_ptr_t< _com_IIID<ICommandText, &__uuidof(ICommandText)> > ICommandTextPtr;
typedef _com_ptr_t< _com_IIID<IRow, &__uuidof(IRow)> > IRowPtr;
typedef _com_ptr_t< _com_IIID<IAccessor, &__uuidof(IAccessor)> > IAccessorPtr;
typedef _com_ptr_t< _com_IIID<IColumnsInfo, &__uuidof(IColumnsInfo)> > IColumnsInfoPtr;
typedef _com_ptr_t< _com_IIID<ICommand, &__uuidof(ICommand)> > ICommandPtr;
typedef _com_ptr_t< _com_IIID<IRowsetInfo, &__uuidof(IRowsetInfo)> > IRowsetInfoPtr;



IDataInitializePtr pIDataInitialize;
IDBInitializePtr pIDBInitialize;
IDBPropertiesPtr pIDBProperties;

hr = CoInitialize(NULL);

//Invoke the OLE DB Service Components with the MSDAINITIALIZE
//provider. Session pooling is enabled while the
//IDataInitialize interface pointer is held.
//hr = CoCreateInstance(CLSID_MSDAINITIALIZE, NULL, CLSCTX_INPROC_SERVER, IID_IDataInitialize, (void**) &pIDataInitialize);
hr = pIDataInitialize.CreateInstance( CLSID_MSDAINITIALIZE ); //, NULL, CLSCTX_INPROC_SERVER );

if( FAILED(hr = CLSIDFromProgID(L"SQLOLEDB", &clsid) ) )
return 0;


const ULONG nProps = 5;
DBPROP InitProperties[nProps];
DBPROPSET rgInitPropSet;

for (ULONG i = 0; i < nProps; i++)
{
VariantInit(&InitProperties[i].vValue);
InitProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
InitProperties[i].colid = DB_NULLID;
}

//Level of prompting performed to complete the connection process.
InitProperties[0].dwPropertyID = DBPROP_INIT_PROMPT;
InitProperties[0].vValue.vt = VT_I2;
InitProperties[0].vValue.iVal = DBPROMPT_NOPROMPT;

//Datasource name.
InitProperties[1].dwPropertyID = DBPROP_INIT_DATASOURCE;
InitProperties[1].vValue.vt = VT_BSTR;
InitProperties[1].vValue.bstrVal = SysAllocString(OLESTR("BERETTA"));

//Userid.
InitProperties[2].dwPropertyID = DBPROP_AUTH_USERID;
InitProperties[2].vValue.vt = VT_BSTR;
InitProperties[2].vValue.bstrVal = SysAllocString(OLESTR("sa"));

//Password.
InitProperties[3].dwPropertyID = DBPROP_AUTH_PASSWORD;
InitProperties[3].vValue.vt = VT_BSTR;
InitProperties[3].vValue.bstrVal = SysAllocString(OLESTR("12345"));

//Catalog : Your database name.
InitProperties[4].dwPropertyID = DBPROP_INIT_CATALOG;
InitProperties[4].vValue.vt = VT_BSTR;
InitProperties[4].vValue.bstrVal = SysAllocString(OLESTR("AUUDatabase"));


//Assign the property structures to the property set.
rgInitPropSet.guidPropertySet = DBPROPSET_DBINIT;
rgInitPropSet.cProperties = nProps;
rgInitPropSet.rgProperties = InitProperties;


//Use the IDataInitialize interface to load the SQLOLEDB provider and create an IDBInitialize interface.
hr = pIDataInitialize->CreateDBInstance(clsid, NULL, CLSCTX_INPROC_SERVER, NULL, IID_IDBInitialize, (IUnknown**)&pIDBInitialize);

pIDBProperties = pIDBInitialize;
hr = pIDBProperties->SetProperties(1, &rgInitPropSet);

hr = pIDBInitialize->Initialize();
if (FAILED(hr))
return 0;


//These variables are for Command creation.

IDBCreateSessionPtr pIDBCreateSession;
IDBCreateCommandPtr pIDBCreateCommand;
IRowsetPtr pIRowset;
ICommandTextPtr pICommandText;
//Here is a sample Select statement. Change it as //neccessary:
LPCOLESTR wSQLString = OLESTR("SELECT * FROM Updates");
LONG cRowsAffected;

//Get the DB Session object.
pIDBCreateSession = pIDBInitialize;
if (FAILED(hr))
return hr;


//Create the session, and get an interface for command creation.
hr = pIDBCreateSession->CreateSession(NULL, IID_IDBCreateCommand,(IUnknown**)&pIDBCreateCommand);
if (FAILED(hr))
return hr;

ICommandPtr pICommand;
//Create the command object.
hr = pIDBCreateCommand->CreateCommand(NULL, IID_ICommandText,(IUnknown**)&pICommandText);
if (FAILED(hr))
return hr;
// pICommandText = pICommand;
hr = pICommandText->SetCommandText(DBGUID_SQL, wSQLString);

//Execute the command.
HACCESSOR hAccessor = DB_NULL_HACCESSOR;
DBPARAMS Params;
LPVOID pData;
Params.pData = pData;
Params.cParamSets = 0;//m_nParams > 0 ? 1 : 0;
Params.hAccessor = DB_NULL_HACCESSOR;

pIRowset = pIDBInitialize;
hr = pICommandText->Execute(NULL,IID_IRowset,NULL,&cRowsAffected,(IUnknown**)&pIRowset);
if (FAILED(hr))
return hr;

IRowsetInfoPtr pIRowSetInfo;
pIRowSetInfo = pIRowset;

ULONG prop;
DBPROPSET *prgProp;
hr = pIRowSetInfo->GetProperties(0,0,&prop,&prgProp);

ULONG num = prgProp->cProperties;


//Create IColumnsInfo Instance
IColumnsInfoPtr pIColumnsInfo;
pIColumnsInfo = pIRowset;

DBCOLUMNINFO* rgColumnInfo = NULL;
ULONG nCols = 0;
LPWSTR pwstrNameBuffer;

pIColumnsInfo->GetColumnInfo(&nCols, &rgColumnInfo, &pwstrNameBuffer);

DBBINDING *rgBindings;
//rgBindings = new DBBINDING[ nCols ];
long cbAlloc = nCols * sizeof(DBBINDING);
rgBindings = (DBBINDING*) ::CoTaskMemAlloc(cbAlloc);

short iAdjustIndex = 0;
ULONG dwOffset = 0;
for( ULONG iCol = 0; iCol < nCols; iCol++ )
{
DBBINDING& b = rgBindings[iCol];
b.iOrdinal = rgColumnInfo[iCol].iOrdinal;
b.dwPart = DBPART_VALUE | DBPART_LENGTH | DBPART_STATUS;
b.obStatus = dwOffset;
b.obLength = dwOffset + sizeof(DBSTATUS);
b.obValue = dwOffset + sizeof(DBSTATUS) + sizeof(ULONG);
b.dwMemOwner = DBMEMOWNER_CLIENTOWNED;
b.eParamIO = DBPARAMIO_INPUT | DBPARAMIO_OUTPUT;//DBPARAMIO_NOTPARAM;
b.bPrecision = rgColumnInfo[iCol].bPrecision;
b.bScale = rgColumnInfo[iCol].bScale;

// Ignore bookmark column
if( (rgColumnInfo[iCol].dwFlags & DBCOLUMNFLAGS_ISBOOKMARK) != 0 ) iAdjustIndex++;
b.dwFlags = DBCOLUMNFLAGS_WRITE;
WORD wType = rgColumnInfo[iCol].wType;
switch( wType ) {
case DBTYPE_CY:
case DBTYPE_DECIMAL:
case DBTYPE_NUMERIC:
b.wType = DBTYPE_STR;
b.cbMaxLen = 100; // Allow 50 characters for conversion
break;
case DBTYPE_STR:
case DBTYPE_WSTR:
#ifdef _UNICODE
b.wType = DBTYPE_WSTR;
#else
b.wType = DBTYPE_STR;
#endif
b.cbMaxLen = max(min((rgColumnInfo[iCol].ulColumnSize + 1UL) * sizeof(TCHAR), 1024UL), 0UL);
break;
default:
b.wType = wType;
b.cbMaxLen = max(min(rgColumnInfo[iCol].ulColumnSize, 1024UL), 0UL);
}

// ROUNDUP on all platforms pointers must be aligned properly
#define ROUNDUP_AMOUNT 8
#define ROUNDUP_(size,amount) (((ULONG)(size)+((amount)-1))&~((amount)-1))
#define ROUNDUP(size) ROUNDUP_(size, ROUNDUP_AMOUNT)

// Update the offset past the end of this column's data
dwOffset = b.cbMaxLen + b.obValue;
dwOffset = ROUNDUP(dwOffset);
}



//Create IAccessor Instance
IAccessorPtr pIAccessor;
DBBINDSTATUS status;
pIAccessor = pIRowset;

hr = pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA, nCols,rgBindings, 0, &hAccessor, &status);// Здесь hr !=0

switch(status) {
case DBBINDSTATUS_BADORDINAL:
break;
case DBBINDSTATUS_UNSUPPORTEDCONVERSION:
break;
case DBBINDSTATUS_BADBINDINFO:
break;
case DBBINDSTATUS_BADSTORAGEFLAGS:
break;
case DBBINDSTATUS_NOINTERFACE:
break;
default:
return 0;
}


HROW *hRow = NULL;
Updates * pRowData;
HROW* rghRows = NULL;
ULONG nRecevied;
hr = pIRowset->GetNextRows(DB_NULL_HCHAPTER, 0, 1, &nRecevied, &hRow);
hr = pIRowset->GetData( *hRow,hAccessor,pRowData );


//=====================================================================

CoUninitialize();

}

Почему не создаетса неверно Аксесор
Может у ковото есть дока по етим вопросам
поготи пожалуста
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.