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

📄 olehandler.cpp

📁 从vc连接到oracle数据库。包括连接
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        // Obtain the column bindings, using column information.
        // *******************************************************************
     
        // Create a DBBINDING array
        pBindings = new DBBINDING[lNumCols];
     
        // Using the ColumnInfo structure, fill  the pBindings array
        for(j = 0; j<lNumCols; j++) {
            
			 // Ordinal positions start at 1
             pBindings[j].iOrdinal = j+1;
             
			 // Buffer offset, re-calculated at the end of this loop
             pBindings[j].obValue = cbColOffset;
            
			 // We're not retrieving the column length
             pBindings[j].obLength = 0;
             
			 // We're not retrieving the column status
             pBindings[j].obStatus = 0;
            
			 // These parameters are for future use...
             pBindings[j].pTypeInfo = NULL;
             pBindings[j].pObject = NULL;
             pBindings[j].pBindExt = NULL;
            
			 // We're Just Retrieving the Value Part
             pBindings[j].dwPart = DBPART_VALUE;
            
			 // The memory will be client owned
             pBindings[j].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
            
			 // This binding does not define a parameter
             pBindings[j].eParamIO = DBPARAMIO_NOTPARAM; 
            
			 // Use the ColumnInfo structure to get the column size
             pBindings[j].cbMaxLen = pDBColumnInfo[j].ulColumnSize;
             pBindings[j].dwFlags = 0;
            
			 // Use the ColumnInfo structure to get the column type
             
			 if ( pDBColumnInfo[j].wType==DBTYPE_NUMERIC)
				 pBindings[j].wType=DBTYPE_STR;
			 else
			    pBindings[j].wType = pDBColumnInfo[j].wType;
            
			 // Use the ColumnInfo structure to get the column precision
             pBindings[j].bPrecision = pDBColumnInfo[j].bPrecision;
             
			 // Use the ColumnInfo structure to get the column scale
             pBindings[j].bScale = pDBColumnInfo[j].bScale;
      
             // Re-calulate the next buffer offset by 
             // Adding the current offset to the maximum column length
             // Obtained from the ColumnsInfo structure
             // Note: When done with this loop, this value will be the 
             // length of the record buffer
             cbColOffset = cbColOffset +pDBColumnInfo[j].ulColumnSize;
			 

        };
      
        // *******************************************************************
	    //  Create an Accessor, using the binding information and the 
        //  CreateAccessor method.
        // *******************************************************************
      
        // Obtain access to the IAccessor Interface
        if(FAILED(hr =pRowset->QueryInterface(IID_IAccessor, (void **) &pIAccessor)))
        {
           AfxMessageBox("Failed to get IAccessor interface in ExecuteQuery()");
		   DispErrorInfo(pRowset,IID_IRowset);
		}
        
		// Create an Accessor handle, using the CreateAccessor method
        if(FAILED(hr =pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA,  // We are retrieving 
                                                                      // row data
                                         lNumCols,                    // The number of 
                                                                      // columns
                                                                      // we are binding
                                         pBindings,                   // The bindings 
                                                                      // structure
                                         0,                           // Not used
                                         &hAccessor,                  // The returned 
                                                                      // accessor
                                                                      //   handle
                                         NULL)))                      // We're not 
                                                                      // returning
                                                                      //   status 
                                                                      // information
     
		{
			AfxMessageBox("Failed to create accesor in ExecuteQuery()");
		   DispErrorInfo(pIAccessor,IID_IAccessor);	
		}
	    //Release interface pointer
		pIAccessor->Release();

		//Return the results to calling function
		return pRowset;
		          
	}
 

	/****************************************************************************
	 This function gets the details of product in two categories 'Orderable'
	 and 'Under Development' using stored procedure with Ref Cursor arguments
	 Steps:
	 1.Specify the SQL statement to call the stored procedure using ODBC escape
	   sequence
     2.Establish connection to database
	 3.Create a command object and set the ORPROP_PLSQLRSet property to true.
	 4.Set the command text to above SQL statement , execute the command
	   and get the results in IMultipleRowsets interface pointer.
     5.Return this pointer to data to dialog box (calling) function and display 
	   results there
     ****************************************************************************/
	IMultipleResults* DBHandler::GetProductInformation()
	{
       
       //Specify the command to execute.
       WCHAR* wCmdString = OLESTR("{Call getproductinfo}");
       
	   /********************
	   Establish connection
	   ********************/
	   // Call a function to initialize and establish connection. 
       InitializeAndEstablishConnection();


	    //Create a session object.
       if(FAILED(pIDBInitialize->QueryInterface(IID_IDBCreateSession,
                                (void**) &pIDBCreateSession)))
	   {
         AfxMessageBox("Failed to obtain IDBCreateSession interface in GetProductInformation()");
		  DispErrorInfo(pIDBInitialize, IID_IDBInitialize);
	   }
       
	   //Open session with database and get a command object
       if(FAILED(pIDBCreateSession->CreateSession(
                                     NULL, 
                                     IID_IDBCreateCommand, 
                                     (IUnknown**) &pIDBCreateCommand)))
	   {
          AfxMessageBox("pIDBCreateSession->CreateSession failed in GetProductInformation()");
		  DispErrorInfo(pIDBCreateSession, IID_IDBCreateSession);
	   }
        //Release interface pointer
        pIDBCreateSession->Release();  
        
	   /*****************************************
	    Create command and Set PLSQLRSet property
		*****************************************/
		//set the DBPropSet struct .This is required to set the ORAPROP_PLSQLRSet
	    //property
	   CDBPropSet propset(ORAPROPSET_COMMANDS); 

		//Get the ICommandProperties interface pointer to set PLSQLRSet property
		
        if(FAILED(pIDBCreateCommand->CreateCommand(NULL, 
                                         IID_ICommandProperties, 
                                         (IUnknown **)&pICommandProperties))) 

		{
			AfxMessageBox("CreateCommand failed in GetProductInformation()");
			DispErrorInfo(pIDBCreateCommand, IID_IDBCreateCommand);
        }

        //Add PLSQLRSet property and its value to DBPROPSET struct and set it for command
		//This is required to tell the provider that a Ref Cursor variable is 
		//being used as parameter to stored procedure.
		//We do not bind this parameter as provider does this automatically
		propset.AddProperty(ORAPROP_PLSQLRSet, true); 
        pICommandProperties->SetProperties(1, &propset); 
        
		/****************************
		Set command text and execute
		*****************************/
		//Get the ICommandText interface pointer
		if(FAILED(pICommandProperties->QueryInterface(IID_ICommandText,(void **)&pICommandText)))
        {

          DispErrorInfo(pICommandProperties,IID_ICommandProperties);
		}

		//Release interface pointer
        pICommandProperties->Release();


         //Use SetCommandText() to specify the command text.
        if(FAILED(pICommandText->SetCommandText(DBGUID_DBSQL, wCmdString)))
		{
          AfxMessageBox("Failed to set command text in GetProductInformation()");
		  DispErrorInfo(pICommandText, IID_ICommandText);
		}

        //Execute the command and get results in IMultipleResults interface pointer
        if(FAILED(hr = pICommandText->Execute(NULL, 
                                    IID_IMultipleResults, 
                                    NULL, 
                                    &cNumRows, 
                                    (IUnknown **) &pIMultipleResults)))
		{
           AfxMessageBox("Failed to execute command in GetPRoductInformation()");
           DispErrorInfo(pICommandText, IID_ICommandText);
		}

        //Return the results to calling function
        return pIMultipleResults;

	}


    /************************************************************************************
	This function inserts the name and description  for the selected product and language
	into the database in following steps
	1.Specify the SQL string to call the stored procedure to insert product details
	2.Specify the parameters and their values for stored procedure in SPROCPARAMS variable
	3.Establish a connection to database and create a session
	4.Set the ORAPROP_NDatatype property to true because NVARCHAR2 is being passed as parameter.
	5.Set the command text to call the database stored procedure
	6.Describe the consumer buffer which is used to pass parameter values to stored procedure
    7.Create an accessor for binding parameters to command to be executed.
	8.Execute the command to insert product details
	9.Uninitialize database session
	Note:The data is inserted if it does not exist else a message saying 'Data Exists' is
	     displayed
	**************************************************************************************/
	void DBHandler::InsertData(int p_productid,CString p_languageid,CString p_tname,CString p_tdescription)
	{

        //The SQL command to execute.
        LPCOLESTR wCmdString = 
           OLESTR("{Call inserttranslateddescription(?,?,?,?,?)}");

		/****************************************************
		Specify parameter to stored procedure and their values
		*****************************************************/
        SPROCPARAMS sprocparams;  //variable to hold parameters
	
		//Set the values of parameters to database stored procedure
		sprocparams.productid= p_productid;
		sprocparams.languageid=p_languageid.AllocSysString();
        sprocparams.tname=p_tname.AllocSysString();
		sprocparams.tdescription=p_tdescription.AllocSysString();
	    sprocparams.check=0;
		
        //set the DBPropSet struct .This is required to set the ORAPROP_NDatatype
		 //property
		CDBPropSet propset(ORAPROPSET_COMMANDS); 
        
		/*******************
		Establish connection
		********************/
		//Initialise and establish connection in a separate function.
        InitializeAndEstablishConnection();

        //Obtain session object from the data source object.
        if(FAILED(pIDBInitialize->QueryInterface(
                                        IID_IDBCreateSession,
                                        (void**) &pIDBCreateSession)))
        {
            AfxMessageBox("Failed to access IDBCreateSession interface in InsertData()");
            DispErrorInfo(pIDBInitialize, IID_IDBInitialize);
        }

		//Create session with the database
        if(FAILED(pIDBCreateSession->CreateSession(
                                         NULL, 
                                         IID_IDBCreateCommand, 
                                         (IUnknown**) &pIDBCreateCommand)))
        {
            AfxMessageBox("pIDBCreateSession->CreateSession failed in InsertData()");
            DispErrorInfo(pIDBCreateSession, IID_IDBCreateSession);
        }
          //Release interface pointer
          pIDBCreateSession->Release();
              
        /***********************************************
		Create command object and set NDatatype property
		************************************************/
		//Create a command object to work with database
        if(FAILED(pIDBCreateCommand->CreateCommand(NULL, 
                                         IID_ICommandProperties, 
                                         (IUnknown **)&pICommandProperties))) 

⌨️ 快捷键说明

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