We help IT Professionals succeed at work.

Automatic renew recordset data in VC++ ADO

hxb
hxb asked
on
1,062 Views
Last Modified: 2008-01-09
VC++ 6.0, SQL Server 2000
I build the recordset of rsVipCard, and just want to find the 'CardID' in database.
But when the data is updated by other user in SQL Server 2000, the rsVipCard not update automatically.
What is the problem?

I use the module of ADO2.CPP
// MODULE: Ado2.cpp
// AUTHOR: Carlos Antollini <cantollini@hotmail.com>
// Copyright (c) 2001-2004. All Rights Reserved.
// Date: August 01, 2005
My code is something like this:

m_pRecordset->CursorLocation = adUseClient
m_pRecordset->CursorType = adOpenDynamic
CADORecordset rsVipCard;
CADODatabase dbVipCard;
rsVipCard = CADORecordset(&dbVipCard);
rsVipCard.Open("select * from VipCard", CADORecordset::openQuery)
strSQL="VipCardID=1245678";
if (rsVipCard.FindFirst (strSQL)!=0)
  //found
else
 //not found


//
// MODULE: Ado2.cpp
//
// AUTHOR: Carlos Antollini <cantollini@hotmail.com>
//
// Copyright (c) 2001-2004. All Rights Reserved.
//
// Date: August 01, 2005
//
// Version 2.20

BOOL CADORecordset::Open(_ConnectionPtr mpdb, LPCTSTR lpstrExec, int nOption)
{      
      Close();
      
      if(strcmp(lpstrExec, _T("")) != 0)
            m_strQuery = lpstrExec;

      ASSERT(!m_strQuery.IsEmpty());

      if(m_pConnection == NULL)
            m_pConnection = mpdb;

      m_strQuery.TrimLeft();
      BOOL bIsSelect = m_strQuery.Mid(0, strlen("Select ")).CompareNoCase("select ") == 0 && nOption == openUnknown;

      try
      {
            m_pRecordset->CursorType = adOpenDynamic;
                  

            m_pRecordset->CursorLocation = adUseClient;
            if(bIsSelect || nOption == openQuery || nOption == openUnknown)
                  m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
                                          adOpenDynamic, adLockOptimistic, adCmdUnknown);  ////huang 2007-09-02 change from adOpenStatic to Dynamic

                  else if(nOption == openTable)
                  m_pRecordset->Open((LPCSTR)m_strQuery, _variant_t((IDispatch*)mpdb, TRUE),
                                          adOpenKeyset, adLockOptimistic, adCmdTable);
            else if(nOption == openStoredProc)
            {
                  m_pCmd->ActiveConnection = mpdb;
                  m_pCmd->CommandText = _bstr_t(m_strQuery);
                  m_pCmd->CommandType = adCmdStoredProc;
                  m_pConnection->CursorLocation = adUseClient;
                  
                  m_pRecordset = m_pCmd->Execute(NULL, NULL, adCmdText);
            }
            else
            {
                  TRACE( "Unknown parameter. %d", nOption);
                  return FALSE;
            }
      }
      catch(_com_error &e)
      {
            dump_com_error(e);
            return FALSE;
      }

      return m_pRecordset != NULL && m_pRecordset->GetState()!= adStateClosed;
}


BOOL CADORecordset::FindFirst(LPCTSTR lpFind)
{
      m_pRecordset->MoveFirst();
      return Find(lpFind);
}


BOOL CADORecordset::Find(LPCTSTR lpFind, int nSearchDirection)
{

      m_strFind = lpFind;
      m_nSearchDirection = nSearchDirection;

      ASSERT(!m_strFind.IsEmpty());

      if(m_nSearchDirection == searchForward)
      {
            m_pRecordset->Find(_bstr_t(m_strFind), 0, adSearchForward, "");
            if(!IsEof())
            {
                  m_varBookFind = m_pRecordset->Bookmark;
                  return TRUE;
            }
      }
      else if(m_nSearchDirection == searchBackward)
      {
            m_pRecordset->Find(_bstr_t(m_strFind), 0, adSearchBackward, "");
            if(!IsBof())
            {
                  m_varBookFind = m_pRecordset->Bookmark;
                  return TRUE;
            }
      }
      else
      {
            TRACE("Unknown parameter. %d", nSearchDirection);
            m_nSearchDirection = searchForward;
      }
      return FALSE;
}
Comment
Watch Question

check this link, http://www.codeproject.com/cs/database/chatter.asp. This talks about the event notification on data change.
Freelance programmer / Consultant
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
hxb

Author

Commented:
I have set  m_pRecordset->CursorType = adOpenDynamic, is it means when there is new update in server side, it will info the ado recordset client?

AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Unless I misunderstand the cursor functionality a client side cursor will not 'see' changes made to the server automatically.  Having the cursor as a server side cursor doesn't mean it will work however - the database drivers would also have to support the server side dynamic cursor.  (With SQL 2000 that support should exist)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.