⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 olehandler.cpp

📁 从vc连接到oracle数据库。包括连接
💻 CPP
📖 第 1 页 / 共 3 页
字号:

		{
			AfxMessageBox("pIDBCreateSession->CreateSession failed in InsertData()");
            DispErrorInfo(pIDBCreateCommand, IID_IDBCreateCommand);
		}
         //Release interface pointer
         pIDBCreateCommand->Release();
         
         //Add NDatatype property and its value to DBPROPSET struct and set it for command
		//This is required to tell the provider that a NVARCHAR2  variable is 
		//being used as parameter to stored procedure.
		
		 propset.AddProperty(ORAPROP_NDatatype, true); 
         pICommandProperties->SetProperties(1, &propset); 

		//Get pointr to ICommandtext interface
		 if(FAILED(pICommandProperties->QueryInterface(IID_ICommandText,(void **)&pICommandText)))
		 {
           DispErrorInfo(pICommandProperties,IID_ICommandProperties);
		 }
          //Release the interface pointer
		  pICommandProperties->Release();
		 
		 //Set the command text.
        if(FAILED(pICommandText->SetCommandText(DBGUID_DBSQL, wCmdString)))
        {
             AfxMessageBox( "Failed to set command text in InsertData()");
             DispErrorInfo(pICommandText, IID_ICommandText);
        }

		
         /* No need to describe command parameters (parameter name, data type
         etc) in DBPARAMBINDINFO structure and then SetParameterInfo(). The
         provider obtains this information by calling appropriate helper
         function.
         */

	     /****************************************************
		 Describe the consumer buffer by filling in the array 
         of DBBINDING structures.  Each binding associates
         a single parameter to the consumer's buffer.
		******************************************************/
        
		for(i = 0; i < nParams; i++)
        {
            acDBBinding[i].obLength = 0;
            acDBBinding[i].obStatus = 0;
            acDBBinding[i].pTypeInfo = NULL;
            acDBBinding[i].pObject = NULL;
            acDBBinding[i].pBindExt = NULL;
            acDBBinding[i].dwPart = DBPART_VALUE;
            acDBBinding[i].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
            acDBBinding[i].dwFlags = 0;
            acDBBinding[i].bScale = 0;
        } //endfor

        
        //Parameter bindings for language id
		acDBBinding[0].iOrdinal = 1;                              //Ordinal position of parameter
        acDBBinding[0].obValue = offsetof(SPROCPARAMS,languageid);//Offset for value of param
        acDBBinding[0].eParamIO = DBPARAMIO_INPUT;                //Parameter type
        acDBBinding[0].cbMaxLen = 100;                            //max length 
        acDBBinding[0].wType = DBTYPE_BSTR;                       //Data type of parameter
        acDBBinding[0].bPrecision = 0;                            //Precision after decimal point
																        
        //Parameter bindings for product id
        acDBBinding[1].iOrdinal = 2;
        acDBBinding[1].obValue = offsetof(SPROCPARAMS,productid);
        acDBBinding[1].eParamIO = DBPARAMIO_INPUT;
        acDBBinding[1].cbMaxLen = 100;
        acDBBinding[1].wType = DBTYPE_I2;
        acDBBinding[1].bPrecision = 0;

		//Parameter bindings for translated name
        acDBBinding[2].iOrdinal = 3;
        acDBBinding[2].obValue = offsetof(SPROCPARAMS,tname);
        acDBBinding[2].eParamIO = DBPARAMIO_INPUT;
        acDBBinding[2].cbMaxLen = 100;
        acDBBinding[2].wType = DBTYPE_BSTR;
        acDBBinding[2].bPrecision = 0;
 
		//Parameter bindings for translated description
		acDBBinding[3].iOrdinal = 4;
        acDBBinding[3].obValue = offsetof(SPROCPARAMS,tdescription);
        acDBBinding[3].eParamIO = DBPARAMIO_INPUT;
        acDBBinding[3].cbMaxLen = 300;
        acDBBinding[3].wType = DBTYPE_BSTR;
        acDBBinding[3].bPrecision = 0;

        //Parameter bindings to check if data existed or was inserted
		acDBBinding[4].iOrdinal =5;
        acDBBinding[4].obValue = offsetof(SPROCPARAMS, check);
        acDBBinding[4].eParamIO = DBPARAMIO_OUTPUT;
        acDBBinding[4].cbMaxLen = sizeof(int);
        acDBBinding[4].wType = DBTYPE_I4;
        acDBBinding[4].bPrecision = 0;

     

        /**************************************************
        //Create an accessor from the above set of bindings.
        //first get the  IAccessor interface pointer
		**************************************************/
		hr = pICommandText->QueryInterface(
                                        IID_IAccessor, 
                                        (void**)&pIAccessor);
        if (FAILED(hr))
        {
            AfxMessageBox("Failed to get IAccessor interface in InsertData()");
			 DispErrorInfo(pICommandText, IID_ICommandText);
        }
        
		//Create accessor for parameters
        hr = pIAccessor->CreateAccessor(DBACCESSOR_PARAMETERDATA,
                                       nParams, acDBBinding, sizeof(SPROCPARAMS), &hAccessor,
        acDBBindStatus);
        if (FAILED(hr))
        {
           AfxMessageBox("Failed to create accessor for the defined parameters in InsertData()");
		   DispErrorInfo(pIAccessor,IID_IAccessor);
        }

		
        /******************
		Execute the command
		*******************
        Fill in DBPARAMS structure for the command execution. This structure
        specifies the parameter values in the command and is then passed 
        to Execute.
        */
        Params.pData = &sprocparams;
        Params.cParamSets = 1;
        Params.hAccessor = hAccessor;   
        
        //Execute the command.
        if(FAILED(hr = pICommandText->Execute(
                                        NULL, 
                                        IID_IRowset, 
                                        &Params,          //Parameters being passed
                                        &cNumRows,        //No of records affected
                                        (IUnknown **) &pRowset)))
        {
			 AfxMessageBox("Command Execution Error in InsertData()");
            DispErrorInfo(pICommandText,IID_ICommandText);
                   
        }
 
		//Free the allocated memory
		pIAccessor->ReleaseAccessor(hAccessor, NULL);
        pIAccessor->Release();
        pICommandText->Release();
	 
		//Check if data was updated or inserted
        if(sprocparams.check==0)
           AfxMessageBox("Product Details Inserted");
		else
			AfxMessageBox("Product Details Updated");
	
	     //Ininitialize the database session
	     UnInitialize();
       
        return;

    
    }


    /********************************************************
	This function uninitializes the session with the database. 
	It also checks if all the interface pointers are released
	********************************************************/
	void DBHandler::UnInitialize()
	{
    
        if(FAILED(pIDBInitialize->Uninitialize()))
        {   
			DispErrorInfo(pIDBInitialize,IID_IDBInitialize);
            /*Uninitialize is not required, but it fails if an interface
            has not been released.  This can be used for debugging.*/
            AfxMessageBox("Problem uninitializing.");
        } //endif


	    pIDBInitialize->Release();
        
        //Release COM.
        CoUninitialize();

	}


   /*******************************************************************
	This function takes in the interface pointer and its GUID and 
	displays the error generated while calling  a function of this 
	interface pointer
	*******************************************************************/
	HRESULT DBHandler:: DispErrorInfo(IUnknown *pErrorInt, GUID ErrorIID)
	{
       ISupportErrorInfo *pSupportErrorInfo;
       IErrorInfo        *pErrorInfo,
                         *pErrorInfoRecord;
       IErrorRecords     *pErrorRecords;
       
       BSTR               pDescription = NULL;
       BSTR               pSource = NULL;
       BSTR               pSQLState = NULL;
       GUID               ErrorGUID;
       ULONG              i,
                          lNumRecs;
       
       HRESULT            retcode = S_OK;
     
       // Obtain access to the ISupportErrorInfo Interface
       if(SUCCEEDED(pErrorInt->QueryInterface(IID_ISupportErrorInfo,
                                         (void **) &pSupportErrorInfo))) {
         // Check if extended error information is available
       if(SUCCEEDED(pSupportErrorInfo->InterfaceSupportsErrorInfo
                      (ErrorIID))) {
          // Access the error info interface
          if(SUCCEEDED(GetErrorInfo(0,&pErrorInfo))) {
             // Retrieve the error records Interface
            if(SUCCEEDED(pErrorInfo->QueryInterface(IID_IErrorRecords,
                                           (void **) &pErrorRecords))) {
               // Retrieve the number of error records
               if(SUCCEEDED(pErrorRecords->GetRecordCount(&lNumRecs))) {
                 for(i=0; i < lNumRecs; i++) {
                    // Get the error info Interface
                    pErrorRecords->GetErrorInfo(i,GetSystemDefaultLCID(),
                                                &pErrorInfoRecord);
                    // Get the error description
                    
					pErrorInfoRecord->GetDescription(&pDescription);
                    
				    // Get the error source
                    pErrorInfoRecord->GetSource(&pSource);
                    // Get the error GUID

					                
					pErrorInfoRecord->GetGUID(&ErrorGUID);
                    
					                    
					//Display the error messaghe
					CString des;
					des.Format("Description: %S\n\n",pDescription);
					AfxMessageBox(des);
                
									
                    // Free the strings
                    SysFreeString(pDescription);
                    SysFreeString(pSource);
                    
                    
                    // Release the Interface
                    pErrorInfoRecord->Release();
                  };
               } else {
                 AfxMessageBox("Can't retrieve the number of error records!");
                  retcode = E_FAIL;
               };
               pErrorRecords->Release();
             } else {
               AfxMessageBox("Can't retrieve the ErrorRecords interface");
               retcode = E_FAIL;
             };
             pErrorInfo->Release();
          } else {
            AfxMessageBox("Can't retrieve the ErrorInfo interface.");
             retcode = E_FAIL;
          };
          pSupportErrorInfo->Release();
       } else {
         AfxMessageBox("Extended Error Information Unavailable!");
         retcode = E_FAIL;
       };
     } else {
       AfxMessageBox("Could Not Obtain Access To The ISupportErrorInfo Interface");
       AfxMessageBox("Additional Error Information Unavailable!");
        retcode = E_FAIL;
     };
	 exit(0);
     return(retcode);
   };

	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -