CDatabase using C++

Hello All,

I am new to using C++ MFC CDatabase class to call a stored procedure.  Can anyone provide a source to learn how to call a stored procedure and return a record set.  I am passing 4 input parameters and return 3 output parameters.

thanks in advance...

regards,

Ken
kenhoppeAsked:
Who is Participating?
 
itsmeandnobodyelseCommented:
You also can use CDatabase::ExecuteSQL.

Here I found the following code sample:
-----------------------------------
Create the following stored procedure in the PUBS Database:


CREATE PROCEDURE get_author_titles @au_id id
AS
BEGIN
SELECT * FROM authors where au_id = @au_id
if @@ROWCOUNT = 0
RAISERROR ('No author found',15,10)
else
SELECT title_id FROM titleauthor WHERE au_id = @au_id
END
Create an MFC AppWizard application with database header support. Create a menu handler.

Paste the following code into the handler:

      CDatabase db;
      db.OpenEx(_T("DSN=Pubs;UID=sa;PWD=;"));
      try {
            db.ExecuteSQL(_T("EXEC get_author_titles '998-72-3567'"));
            db.ExecuteSQL(_T("EXEC get_author_titles '998-72-3568'"));
      }
      catch (CDBException* pEx)
      {
            TCHAR buff[80];
            pEx->GetErrorMessage(buff,80);
            AfxMessageBox(buff);
            pEx->Delete();
      }

------------- end sample ---------------------------

The sample is how to handle an exception between two calls of a stored procedure but it may help nevrtheless.

Regards, Alex
0
 
jkrCommented:
Take a look at http://www.codeproject.com/database/myrecordset.asp ("Two classes for parameterized SQL statement and stored procedure")
0
 
itsmeandnobodyelseCommented:
You can use the CRecordset class:

  CRecordset rset(pdb);   // pass the CDatabase pointer
  if (!rset.Open(CRecordset::snapshot, "{call OverDueAccts}"))
  {
          // something goes wrong
  }


Regards, Alex
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.