Microsoft Knowledge Base Email Alertz

CDynamicParameterAccessor is an OLEDB Template class designed to execute stored procedures. This class is similar to the CDynamicAccessor class, but it obtains the parameter information to be set by calling the ICommandWithParameters interf

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 198519 - Last Review: March 14, 2005 - Revision: 1.3

How To Execute Stored Procedure Using CDynamicParmeterAccesor

This article was previously published under Q198519

On This Page

SUMMARY

CDynamicParameterAccessor is an OLEDB Template class designed to execute stored procedures. This class is similar to the CDynamicAccessor class, but it obtains the parameter information to be set by calling the ICommandWithParameters interface.

This article demonstrates how to use this class for executing a SQL Server stored procedure and getting the output parameter values.

MORE INFORMATION

SQL Scripts


   Create Table Students
   ( id int primary key,


     Name varchar(20),
     Age int)


   Insert into Students values (1,'John',12)
   Insert into Students values (2,'Ram',13)
   Insert into Students values (3,'Keshav',10)
   Insert into Students values (4,'David',9)


   CREATE PROCEDURE sp_getStudent
   @id int ,
   @outparm int OUTPUT
   AS
   select  * from students where id = @id
   select @outparm = 99
				

Visual C++ Code for Executing this Stored Procedure

   void ExecProc()
   {
      HRESULT hr;
       CDataSource db;
	 // Change the arguments of Open() call as appropriate.
	 CDBPropSet propset(DBPROPSET_DBINIT);
	propset.AddProperty(DBPROP_INIT_DATASOURCE, L"SQLServer1");
	propset.AddProperty(DBPROP_INIT_CATALOG, L"pubs");
	propset.AddProperty(DBPROP_AUTH_USERID, L"sa");
	propset.AddProperty(DBPROP_AUTH_PASSWORD, L"");
	hr = db.Open("SQLOLEDB", &propset);
      if (FAILED(hr))
		return;



      CSession m_session;
      m_session.Open(db);



      // Create a command with multiple results. This is done to get the
      // output parameter.
      CCommand<CDynamicParameterAccessor,CRowset,CMultipleResults> Rs;
      hr = Rs.Create(m_session,"exec sp_getstudent ?,? OUT");
      hr = Rs.Prepare();



      void* pDummy;



      // Bind the parameters.
      hr = Rs.BindParameters(&Rs.m_hParameterAccessor,
                                 Rs.m_spCommand,&pDummy);



      long lOutVal;



      int nParamValue = 2;
      char *pData;



      // Set the parameter values.
      Rs.SetParam((ULONG)1,&nParamValue);



      // Open the command.
      hr=Rs.Open(NULL,NULL,0);



      // Call this function to bind the output resultset.
      Rs.Bind();
     hr = Rs.MoveFirst();



      // Get the resultset.
      while (hr == S_OK)
      {
         pData = (char*)Rs.GetValue(2L);
       hr = Rs.MoveNext();
      }




      // Now we get the output parameter.
      hr=Rs.GetNextResult(&lOutVal);



      // Get the output parameter value.
      Rs.GetParam((ULONG)2, &lOutVal);



      m_session.Close();
      db.Close();


   }


NOTE: To set a string parameter, you may want do something like the following instead of calling SetParam(). 
You need to get a pointer to the buffer and then copy the new string value into it.

   DBTYPE dbtype;
   StudRs.GetParamType((ULONG)1,&dbtype);
   char *pParam;

   if(dbtype == DBTYPE_STR)
   {
      // This gives the address of the buffer of the parameter
      pParam = (char *)StudRs.GetParam( (ULONG) 1 );
      // copy the parameter value
      strcpy(pParam,"MacFeather");
   }
   }
				

REFERENCES

Please see the CDynamicParameterAccessor documentation on MSDN.

APPLIES TO
  • Microsoft OLE DB 2.7, when used with:
    • Microsoft Visual C++ 6.0 Enterprise Edition
    • Microsoft Visual C++ 6.0 Professional Edition
    • Microsoft Visual C++, 32-bit Learning Edition 6.0
Keywords: 
kbconsumer kbdatabase kbdtl kbhowto kbprovider KB198519
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate