CDatabase using C++

Posted on 2007-10-10
Last Modified: 2013-11-20
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...


Question by:kenhoppe
    LVL 86

    Expert Comment

    Take a look at ("Two classes for parameterized SQL statement and stored procedure")
    LVL 39

    Expert Comment

    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
    LVL 39

    Accepted Solution

    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
    SELECT * FROM authors where au_id = @au_id
    if @@ROWCOUNT = 0
    RAISERROR ('No author found',15,10)
    SELECT title_id FROM titleauthor WHERE au_id = @au_id
    Create an MFC AppWizard application with database header support. Create a menu handler.

    Paste the following code into the handler:

          CDatabase db;
          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];

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

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

    Regards, Alex

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
    Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now