none
ole db-批处理的问题 RRS feed

  • 问题

  • Hi,Guys

    我现在用ole db操作数据库,当我插入4条记录的时候,如果有一个记录失败了,我怎么得到这个纪录的偏移量, 例如:如果是第二条记录失败了,那怎么能知道失败的第二条记录呢。我的代码如下:

    [CODE]
        CComPtr<IDBCreateSession> pIDBCreateSession;
        CComPtr<IDBCreateCommand> pIDBCreateCommand;
        CComPtr<ICommandText> pICommandText;
        CComPtr<ICommandPrepare> pICommandPrepare;
        CComPtr<IAccessor>               pIAccessor;
        HACCESSOR hParamAccessor=DB_NULL_HACCESSOR;
        DBPARAMS Params={0};
        LPSTR pRowValues=NULL;

       // map binding type
       MapBindingType(...);

       if(FAILED(hResult=_pIDBInitialize->QueryInterface(IID_IDBCreateSession,(void**)&pIDBCreateSession));
        if(FAILED(hResult=pIDBCreateSession->CreateSession(NULL,IID_IDBCreateCommand,(IUnknown**)&pIDBCreateCommand)));

        if(FAILED(hResult=pIDBCreateCommand->CreateCommand(NULL,IID_ICommandText,(IUnknown**)&pICommandText)));
        if(FAILED(hResult=pICommandText->SetCommandText(DBGUID_DBSQL,CONVERT_CHAR(sqlString._sqlBuffer))));

       if(FAILED(hResult=pICommandText->QueryInterface(IID_ICommandPrepare,(void**)&pICommandPrepare)))
       if(FAILED(hResult=pICommandPrepare->Prepare(max(64*1024,min(1024*1024*40L,pDBRds->numRecords*cbRow)))));

       if(FAILED(hResult=pICommandText->QueryInterface(IID_IAccessor,(void**)&pIAccessor)));

        if(FAILED(hResult=pIAccessor->CreateAccessor(DBACCESSOR_PARAMETERDATA,pDBRds->numFields,_oleDBBinding,cbRow,&hParamAccessor,NULL)))

        long typeTransaction(0);

        CComPtr<ITransactionLocal> pITransactionLocal;
        if(SUCCEEDED(pIDBCreateCommand->QueryInterface(IID_ITransactionLocal,(void**)&pITransactionLocal)));

    if(SUCCEEDED(pITransactionLocal->StartTransaction(ISOLATIONLEVEL_REPEATABLEREAD,0,NULL,NULL)))
    typeTransaction = 2;

    Params.pData = pRowValues;
    Params.cParamSets = __oledb_maxbatch__;
    Params.hAccessor = hParamAccessor;

    // map binding data
    MapOledbData(...);

    if(SUCCEEDED(hResult=pCommand->Execute(NULL,IID_NULL,pParams,&cRowsAffected,NULL)));

    pITransactionLocal->Commit(FALSE,0,0);

    if(cRowsAffected != __oledb_maxbatch__)
    {
    // How to get offsest of the error records?
    }
    [/CODE]

    2010年11月15日 9:39

全部回复

  • If you’re using a batch, you can try verifying *pcRowsAffected of IMultipleResults::GetResult  for each statement in the batch.
    • 已建议为答案 Zheng Chen 2010年11月24日 1:07
    2010年11月24日 1:07