ADO

From: Pardeep Kadian (anonymous_at_discussions.microsoft.com)
Date: 02/26/04


Date: Thu, 26 Feb 2004 05:26:05 -0800

Dear ,
I m in Urgent Need of Your Guidence.
I m writing an very simple Code to access SqlServer using SQLOLEDB Provider
And Every Thing is here , even if you have to try The same code , just replace
Data Source and "Initial Catalog" Values in Connection String

The problem is it is showing Successfull execution of Open(....) , for Connection as well as Recordset, but when i m tring to access records it throws an error which is

/*************************
Error
        Code = 800a0e78
        Code meaning = Unknown error 0x800A0E78
        Source = ADODB.Recordset
        Description = Operation is not allowed when the object is closed.
************************************************************************************************/

Can anyOne Plz Let Me know the issue.
Thanx & Regards,
Pardeep Kadian

here it is

// BeginOpenCpp
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" no_namespace rename("EOF", "EndOfFile")

#include <oledb.h>
#include <stdio.h>
#include <conio.h>

// Function declarations
inline void TESTHR(HRESULT x) {if FAILED(x) _com_issue_error(x);};
void OpenX(void);
void PrintProviderError(_ConnectionPtr pConnection);
void PrintComError(_com_error &e);

///////////////////////////////////////////////////////////
// //
// OpenX Function //
// //
///////////////////////////////////////////////////////////

void OpenX(void)
{
    // Define ADO object pointers.
    // Initialize pointers on define.
    // These are in the ADODB:: namespace
     _RecordsetPtr pRstEmployee = NULL;
     _ConnectionPtr pConnection = NULL;

    // Define string variables.
    _bstr_t strCnn("Provider=SQLOLEDB.1;Password=test;Persist Security Info=True;User ID=test;Initial Catalog=test;Data Source=EXCHANGE");
        _bstr_t szValue;
        _variant_t nVtIndex;
        nVtIndex.vt= VT_I2 ;
                
    // Define Other Variables.
    HRESULT hr = S_OK;
    DBDATE varDate;

    try
    {
        // open connection and record set
        TESTHR(pConnection.CreateInstance(__uuidof(Connection)));
        HRESULT hr = pConnection->Open(strCnn,"","",adConnectUnspecified);

        TESTHR(pRstEmployee.CreateInstance(__uuidof(Recordset)));
        hr = pRstEmployee->Open("Select * from PardeepDummy",
            _variant_t((IDispatch *)pConnection,true), adOpenForwardOnly,
            adLockReadOnly, adAsyncExecute);

        while(!pRstEmployee->EndOfFile)
                        {
                                nVtIndex.iVal = 1;
                                //mbstowcs
                                szValue=pRstEmployee->Fields->GetItem(nVtIndex)->Value;
                        // CString szValuestr((char *)szValue);
                                
                  
                                 pRstEmployee->MoveNext();
                                
                        }
        // Clean up objects before exit.
        pRstEmployee->Close();
        pConnection->Close();
    }

    catch(_com_error &e)
    {
        // Notify the user of errors if any.
        // Pass a connection pointer accessed from the Connection.
        PrintProviderError(pConnection);
        PrintComError(e);
    }
}

///////////////////////////////////////////////////////////
// //
// PrintProviderError Function //
// //
///////////////////////////////////////////////////////////

void PrintProviderError(_ConnectionPtr pConnection)
{
    // Print Provider Errors from Connection object.
    // pErr is a record object in the Connection's Error collection.
    ErrorPtr pErr = NULL;

    if( (pConnection->Errors->Count) > 0)
    {
        long nCount = pConnection->Errors->Count;
        // Collection ranges from 0 to nCount -1.
        for(long i = 0;i < nCount;i++)
        {
            pErr = pConnection->Errors->GetItem(i);
            printf("\t Error number: %x\t%s", pErr->Number,
                pErr->Description);
        }
    }
}

///////////////////////////////////////////////////////////
// //
// PrintComError Function //
// //
///////////////////////////////////////////////////////////

void PrintComError(_com_error &e)
{
   _bstr_t bstrSource(e.Source());
   _bstr_t bstrDescription(e.Description());

    // Print COM errors.
   printf("Error\n");
   printf("\tCode = %08lx\n", e.Error());
   printf("\tCode meaning = %s\n", e.ErrorMessage());
   printf("\tSource = %s\n", (LPCSTR) bstrSource);
   printf("\tDescription = %s\n", (LPCSTR) bstrDescription);
}
// EndOpenCpp

///////////////////////////////////////////////////////////
// //
// Main Function //
// //
///////////////////////////////////////////////////////////

void main()
{
    if(FAILED(::CoInitialize(NULL)))
        return;

    OpenX();

    ::CoUninitialize();
        getch();
}



Relevant Pages

  • Re: ADO
    ... > I m writing an very simple Code to access SqlServer using SQLOLEDB ... Connection as well as Recordset, but when i m tring to access records it ... > void OpenX; ...
    (microsoft.public.data.ado)
  • Re: ADO
    ... > I m writing an very simple Code to access SqlServer using SQLOLEDB ... Connection as well as Recordset, but when i m tring to access records it ... > void OpenX; ...
    (microsoft.public.vc.database)
  • Re: ADO
    ... > I m writing an very simple Code to access SqlServer using SQLOLEDB ... Connection as well as Recordset, but when i m tring to access records it ... > void OpenX; ...
    (microsoft.public.data.oledb)
  • ADO
    ... I m writing an very simple Code to access SqlServer using SQLOLEDB Provider ... The problem is it is showing Successfull execution of Open, for Connection as well as Recordset, but when i m tring to access records it throws an error which is ... void OpenX; ... void PrintProviderError(_ConnectionPtr pConnection); ...
    (microsoft.public.vc.database)
  • ADO
    ... I m writing an very simple Code to access SqlServer using SQLOLEDB Provider ... The problem is it is showing Successfull execution of Open, for Connection as well as Recordset, but when i m tring to access records it throws an error which is ... void OpenX; ... void PrintProviderError(_ConnectionPtr pConnection); ...
    (microsoft.public.vc.database)