how to get the SQL statement from which a recordset is created??

Posted on 2005-05-14
Last Modified: 2013-11-20
i am using ADO _RecordsetPtr and _ConnectionPtr to create recordset from SQL statement...
say i created a rsTemp from a SQL statement "select * from client"....

now i have that can i find that SQL statement from which rsTemp object is created???

sample code
_ConnectionPtr m_Connection;
_RecordsetPtr m_Recordset;

CString strConnection = "Provider=Microsoft.Jet.OLEDB.4.0; Persist Security Info=False; Data Source= 'c:\temp.mdb'";

HRESULT hr = m_Connection.CreateInstance(__uuidof(Connection));      
BSTR bstrDataSource = strConnection.AllocSysString();


CString strSQL = "Select * From Client"
now my Question is ..... from that m_Recordset how can i get the base SQL statement from which this is created?? in this senario the ans will be "Select * From Client"......

but how??? to find out??
Question by:shurid
    LVL 6

    Expert Comment

    // get the source sql of the recordset

    _variant_t  vSrc  = m_Recordset->GetSource();
    CString strSQL = vSrc.bstrVal;

    LVL 3

    Accepted Solution

    Use The GetSource Function:

    _ConnectionPtr  pConnection          = NULL;
       _RecordsetPtr  pRstTitles        = NULL;
       _RecordsetPtr  pRstPublishers      = NULL;
       _RecordsetPtr  pRstPublishersDirect    = NULL;
       _RecordsetPtr  pRstTitlesPublishers    = NULL;
       _CommandPtr     pCmdSQL          = NULL;

            // Open a connection.
            pConnection->Open (strCnn, "", "", adConnectUnspecified);

            // Open a recordset based on a command object.
            pCmdSQL->ActiveConnection = pConnection;
            pCmdSQL->CommandText = strCmdSQL;
            pRstTitles = pCmdSQL->Execute(NULL,NULL,adCmdText);

            // Open a recordset based on a a table
            pRstPublishers->Open ("publishers",
                _variant_t((IDispatch *) pConnection, true),
                adOpenForwardOnly, adLockReadOnly, adCmdTable);

            // Open a recordset based on a table
            pRstPublishersDirect->Open ("publishers",
                _variant_t((IDispatch *) pConnection, true),
                adOpenForwardOnly, adLockReadOnly, adCmdTableDirect);

            // Open a recordset based on a SQL string.
                _variant_t((IDispatch *) pConnection, true),
                adOpenForwardOnly, adLockReadOnly, adCmdText);

            // Use the Source property to display the source of
            // each recordset.
            printf("rstTitles source: \n%s\n\n",
                (LPCSTR)(_bstr_t) pRstTitles->GetSource().bstrVal);
            printf("rstPublishers source: \n%s\n\n",
                (LPCSTR)(_bstr_t) pRstPublishers->GetSource().bstrVal);
            printf("rstPublishersDirect source: \n%s\n\n",
                (LPCSTR)(_bstr_t) pRstPublishersDirect->GetSource().bstrVal);
            printf("rstTitlesPublishers source: \n%s\n\n",
                (LPCSTR)(_bstr_t) pRstTitlesPublishers->GetSource().bstrVal);

    LVL 1

    Author Comment

    Sorry for my Acception Delay ... cos "NawalKishore1976" u gave me answer after 2days and by then i got the solution by my self and forget to close the Q... ur answer is right so i am giving u the point....and thankx a lot

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
    Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    728 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

    18 Experts available now in Live!

    Get 1:1 Help Now