CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Error in Open recordset

  1. #1
    Join Date
    Sep 2007
    Posts
    21

    Error in Open recordset

    I am using VC++ and MYSQL. I have a class member _RecordsetPtr pRS which I use for fetching records of database rows. I have the following code in a loop.

    if(pRS) {
    pRS->Close()
    pRS = NULL;
    }

    pRS.CreateInstance (__uuidof (Recordset))

    pCmd->CommandText = strSQL;
    hr = pRS->Open ((IDispatch *) pCmd, vtMissing, adOpenForwardOnly, adLockOptimistic, adCmdUnknown);

    Here pRS->Open gives me an "Unspecified Error" from MYSQL. What is the way to loop through, executing different SQL's and fetching records.

    regards,
    Jatin.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,724

    Re: Error in Open recordset

    Quote Originally Posted by jatinkulkarni
    I am using VC++ and MYSQL. I have a class member _RecordsetPtr pRS which I use for fetching records of database rows. I have the following code in a loop.
    Code:
    if(pRS) {
    	pRS->Close()
    	pRS = NULL; // why?????  :confused: 
    }
    Why do you set pRS to NULL?
    Search MSDN for VC++ examples using ADO _RecordsetPtr Open Close
    Always use try/catch(_com_error &e) blocks!
    Victor Nijegorodov

  3. #3
    Join Date
    Aug 2007
    Location
    Farnborough, Hants, UK
    Posts
    45

    Re: Error in Open recordset

    As mentioned above, use a try / catch block for _com_error. In that catch block, add these lines:

    if ( (pCmd->ActiveConnection->Errors->Count) > 0)
    {
    long nCount = pCmd->ActiveConnection->Errors->Count;

    // Collection ranges from 0 to nCount -1.
    for ( long i = 0 ; i < nCount ; i++ )
    {
    ErrorPtr pErr = pCmd->ActiveConnection->Errors->GetItem(i);

    CString strTemp ;
    strTemp.Format("Error number: %x\t%s\n", pErr->Number, (LPCSTR)pErr->Description);
    ::OutputDebugString(strTemp);
    }
    }

    (apologies if it wraps)

    I have just discovered this myself - what it will do is log a more useful error message to your console about the nature of the error, assuming mySQL gives one. We're using SQL Server here but we get the same "unspecified error" annoyance and this has been so much more useful to us.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)