如何使用ADO删除数据? [英] How to delete data using ADO ?

查看:99
本文介绍了如何使用ADO删除数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用ADO删除数据,请您帮帮我吗?
我编写的代码如下

I am not able to delete data using ADO ,Will you please help me ?
Code I written as follows

#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();
}

推荐答案

除了我的注释中的注释(指定光标和锁定模式,请使用Update()而不是UpdateBatch()),您也可以使用SQL命令删除特定记录集的方法:
Besides the notes from my comments (specify cursor and lock modes, use Update() rather than UpdateBatch()), you may also use a SQL command to delete a specific recordset:
ADODB::_CommandPtr pCmd = NULL;  
HRESULT hr = pCmd.CreateInstance(__uuidof( ADODB::Command));
if (FAILED(hr))
{
    throw _com_error(hr);
}   
_bstr_t bstrCmd = "DELETE FROM Table1 WHERE EmpId=1;";
pCmd->CommandText = bstrCmd;
pCmd->ActiveConnection = pConnnect;  
pCmd->Execute(NULL, NULL, ADODB::adCmdText);



使用Windows 7 SP1时,另请参见文章您的ADO已损坏 [ ^ ]和此评论[ ^ ].



When using Windows 7 SP1, see also the article Your ADO is broken[^] and this comment [^].


这篇关于如何使用ADO删除数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆