ADO Connection to oracle with c++

Discussion in 'C++' started by rahulonly4u, Jan 11, 2011.

  1. rahulonly4u

    rahulonly4u New Member

    Joined:
    Sep 6, 2010
    Messages:
    29
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    software developer
    Location:
    india
    Code:
    # include "stdafx.h"
    #include "MyDBConnection.h"
    
    _ConnectionPtr m_pConn;
    _RecordsetPtr pRecordset;
    _CommandPtr pCommand; 
    _ParameterPtr pParam1;
    MyDBConnection::MyDBConnection()
    {
    	m_pConn = NULL;
    }
    void MyDBConnection::Connect()
    { 
    	// COM initialization
    	CoInitialize (NULL);
    	 
    	// Stablishing a connection to the datasource
    	try	
    	{
    		HRESULT hr = m_pConn.CreateInstance (__uuidof (Connection));
    
    		if (FAILED (hr))
    		{
    			AfxMessageBox (_T("Can't create intance of Connection"));
    		}	
    		if (FAILED (m_pConn->Open (_bstr_t ("Provider=OraOLEDB.Oracle;PLSQLRSet=1; Data Source=NZWRHK;"),
                                               _bstr_t ("scott"), _bstr_t ("tiger"), adModeUnknown)))
            {
                    AfxMessageBox (_T("Can't open datasource"),0,0);
            }
    
    	 
    	}
    	catch ( _com_error &e )
    	{
    		_bstr_t bstrSource (e.Source());
    		_bstr_t bstrDescription (e.Description());
    		TRACE ( "Exception thrown for classes generated by #import" );
    		TRACE ( "\tCode = %08lx\n", e.Error ());
    		TRACE ( "\tCode meaning = %s\n", e.ErrorMessage ());
    		TRACE ( "\tSource = %s\n", (LPCTSTR) bstrSource);
    		TRACE ( "\tDescription = %s\n", (LPCTSTR) bstrDescription);
    
    		AfxMessageBox ((LPCTSTR) bstrDescription);
    	}
    	catch (...)
    	{
    		TRACE ( "*** Unhandled Exception ***" );
    	}
    	return ;
    }
    
    long MyDBConnection::DBGetRS(CString* strText, CString& strRS, int intDBType)
    {
    	// TODO: Add your control notification handler code here
    	
    	if (m_pConn == NULL)		Connect();
    
    	try	
    	{
    		variant_t vt;
            vt.SetString("2");  
    		
    		_CommandPtr pCommand;
    		
    		strRS = _T("");
    		HRESULT hr = pCommand.CreateInstance (__uuidof (Command));
             
    		if (FAILED (hr))
    		{
    		//	printf("\n this is return 1===%s\n",strText);
    			CString strInfo;
    			AfxMessageBox(  _T("Can't create an instance of Command"));
    			//OnInfoError(&strInfo);
    			return 1;
    		}
    		
            AfxMessageBox(_T("command created"));
    		
    		 pCommand->ActiveConnection = m_pConn;
    		 //pCommand->Properties->GetItem("PLSQLRSet")->Value = true;
             CString strCommandText,strQuery;
    		 strQuery = CString(*strText);
    		
        //###########################################################################
    	     
    
      	 pParam1 = pCommand->CreateParameter( _bstr_t ("pParam1"),adSmallInt,adParamInput, sizeof(int),( VARIANT ) vt);
        pCommand->Parameters->Append(pParam1); 
        pRecordset.CreateInstance (__uuidof (Recordset));
    
     
    
    
      pCommand->CommandText = "CALL GetEmpRS1(?)";
      strCommandText.Format(_T("%s"),  strQuery);	  
     pRecordset = pCommand->Execute(NULL, NULL, 
    							 adCmdStoredProc | adCmdUnspecified ); */
           
    
     CString strContent;
     while( pRecordset !=NULL ) 
     {
    		
        while( !pRecordset->GetadoEOF() )
        {
    	//traverse through all the records of current recordset...
    				 
           strContent = (char *)(_bstr_t)pRecordset->Fields->GetItem("item")->Value.bstrVal;
          AfxMessageBox(strContent);
        if(strContent.IsEmpty())
    		 AfxMessageBox(_T("no data"));
       else
    	strRS += strContent;
    	 AfxMessageBox(strContent);					 
    	 pRecordset->MoveNext ();
       } 
    		
         long lngRec = 0;
         pRecordset = pRecordset->NextRecordset((VARIANT *)lngRec);
       } 
        pRecordset->Close ();
    }
      
    		 
    	catch( _com_error &e )
    	{
    		_bstr_t bstrSource(e.Source());
    		_bstr_t bstrDescription(e.Description());
    		CString strInfo;
    		strInfo.Format(_T("%s %s"),bstrSource,bstrDescription);
    		AfxMessageBox(strInfo);
    		//OnInfoError(&strInfo);
    		return 1;
    	}
    	catch (...)
    	{
    		CString strInfo;
    		strInfo.Format(_T("Unhandled Exception in OnQuery"));
    		//OnInfoError(&strInfo);
    		return 1;
    	}
    	return 0;
    }





    AND THID Is MY SP IN ORACLE DB



    create or replace
    PROCEDURE GetEmpRS1 (ResultSet_Cursor1 OUT SYS_REFCURSOR,
    ResultSet_Cursor2 OUT SYS_REFCURSOR,
    PARAM IN STRING) AS
    BEGIN
    open ResultSet_Cursor1 for
    SELECT empname "item" FROM employee@MYDB;

    open ResultSet_Cursor2 for
    SELECT empadd "item" FROM employee@MYDB;

    END GetEmpRS1;






    I am getting error " bstrDescription {"ORA-06553: PLS-306: wrong number or types of arguments in call to 'GETEMPRS1'" (1)}
    " what may be the cause other sp which dont have reffrence cursor is working fine even with this same sp if i remove this rdfcursor work fine
     
    Last edited by a moderator: Jan 11, 2011
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Which version of OraOLEDB are you using? (Use the Oracle Installer - click Installed Products, and don't forget to check for any patchsets installed. Do not rely on DLL version strings; these are not accurate. And give the full version number, e.g. 9.2.0.7.0, not just a family name, e.g. "9i")
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice