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();
}
Re: How to delete data using ADO ?
- Please, edit your post adding Code tags around code snippet. Otherwise you code is not readable.
- Defime "not able to delete data using ADO". What exact problem do you have?
- Have a look at Delete Method Example (VC++) in MSDN.
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
Re: How to delete data using ADO ?
Re: How to delete data using ADO ?
Microsoft example is too complicated to understand,will you please point out where I am wrong?
Re: How to delete data using ADO ?
Quote:
Originally Posted by
shivditya
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
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::Delete method. I usually use DELETE FROM... query.
Re: How to delete data using ADO ?
Have you tried
Code:
if(pRecSet->Supports(adDelete))
{
...Ok to delete record...
}