CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2007
    Posts
    25

    Question How to delete data using ADO ?

    I am not able to delete data using ADO ,Will you please help me ?
    Code I written as follows
    Code:
    #include <iostream>
    #include <tchar.h>
    #import " C:/Program Files/Common Files/System/ado/msado15.dll"  \
         rename( "EOF", "AdoNSEOF" )
     
        _bstr_t bstrConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\VCPrograms\\Other Trials\\Database2.accdb;";
         HRESULT hResult = CoInitialize( 0 );
        if( FAILED( hResult ))
        {
            return;
        }
        try
        {
    	 ADODB::_ConnectionPtr pConnect("ADODB.Connection");
            hResult = pConnect->Open( bstrConnect, "admin", "",    
                      ADODB::adConnectUnspecified );
            if (SUCCEEDED(hResult))
            {
    	   _bstr_t query = "SELECT * FROM Table1 WHERE EmpId = 1;";
                ADODB::_RecordsetPtr pRecSet( "ADODB.Recordset" );
                hResult = pRecSet->Open( query, _variant_t((IDispatch *) pConnect, true), ADODB::adOpenUnspecified,ADODB::adLockUnspecified, ADODB::adCmdText);
    	    if( SUCCEEDED( hResult ))
               {
    	            pRecSet->Delete(ADODB::adAffectCurrent);
    	           pRecSet->UpdateBatch(ADODB::adAffectCurrent);
    	    }
                 pRecSet->Close();
                pConnect->Close();
    	}
        }
        catch( _com_error& e )
        {
            // Handle Exception
        }
     
        // Release COM
        CoUninitialize();
    }
    Last edited by shivditya; April 2nd, 2012 at 10:07 AM.

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

    Re: How to delete data using ADO ?

    1. Please, edit your post adding Code tags around code snippet. Otherwise you code is not readable.
    2. Defime "not able to delete data using ADO". What exact problem do you have?
    3. Have a look at Delete Method Example (VC++) in MSDN.
    Victor Nijegorodov

  3. #3
    Join Date
    May 2007
    Posts
    25

    Question Re: How to delete data using ADO ?

    Will you please tell me how to add code tag?you are right,though my code is well indented as soon as i save it becomes unindented.
    The exact problem I am facing is that Deletion is not taking place in database

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to delete data using ADO ?

    Victor Nijegorodov

  5. #5
    Join Date
    May 2007
    Posts
    25

    Question Re: How to delete data using ADO ?

    Microsoft example is too complicated to understand,will you please point out where I am wrong?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: How to delete data using ADO ?

    Quote Originally Posted by shivditya View Post
    Microsoft example is too complicated to understand
    No, it is NOT complicated!
    Just try it to run and then to understand what, where and why is executed.
    Then you'll be able to omit some unneeded for your purposes parts of code...

    Quote Originally Posted by shivditya View Post
    will you please point out where I am wrong?
    No, I won't.
    You should first debug your code to understand what happens, which part does work, which not. For instance, is the connection opened? Is recordset opened? what is the returned record count? Does the Delete(...) run? Does the UpdateBatch(...) run?
    And so on...

    Besides, I (almost) never use _RecordsetPtr:elete method. I usually use DELETE FROM... query.
    Victor Nijegorodov

  7. #7
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: How to delete data using ADO ?

    Have you tried
    Code:
    if(pRecSet->Supports(adDelete))
    {
       ...Ok to delete record...
    }
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured