现在的位置: 首页 > 综合 > 正文

ADO数据库编程

2013年04月07日 ⁄ 综合 ⁄ 共 4369字 ⁄ 字号 评论关闭
导入ADO接口
#import "C:/Program Files/Common Files/System/ado/msado15.dll" no_namespace rename("EOF", "adoEOF") rename("BOF", "adoBOF")
#include <icrsint.h>  // ADO C/C++ Record Binding Definitions

#define TESTHR(x) if FAILED(x) _com_issue_error(x)

打开连接
打开数据库的链接的步骤:
1)获取COM对象Connection接口指针。
2)调用Connection对象的open方法,此时涉及链接串问题——数据库的认证方式。
下面是个简单的演示:

    _ConnectionPtr GetConnection()
    
{
         
// 1)连接字符串
        const _bstr_t bstrConn(_T("Provider='sqloledb';Data Source='SQLServer';
                                                     Initial Catalog='MyTestDatabase';User ID='User1'; Password='';"));
        // 或者用Windows集成认证
        
//const _bstr_t bstrConn(_T("Provider='sqloledb';Data Source='SQLServer';
        
//                                             Initial Catalog='MyTestDatabase';Integrated Security='SSPI';"));
       _ConnectionPtr spConnection;          
       TESTHR(spConnection.CreateInstance(_T(
"ADODB.Connection")));
       TESTHR(spConnection
->Open(bstrConn, """", adConnectUnspecified));

        
return spConnection;
    }


查询记录
下面是个简单的演示表:
 
通过普通方式访问记录

HRESULT QueryAllWithSP()
{
    HRESULT hr 
= S_OK;
    
try
    {
          _variant_t vtRecordsAffected;
         _RecordsetPtr spRecordset;
         _CommandPtr spCommand;
         TESTHR(command_.CreateInstance(_T(
"ADODB.Command")));         
        command_
->CommandText = _T("spUserInfoQueryALL");
        command_
->CommandType = adCmdStoredProc;     
        command_
->ActiveConnection = GetConnection();
        spRecordset 
= command_->Execute(&vtRecordsAffected, NULL, adCmdStoredProc);

        spRecordset->MoveFirst();
        _variant_t vtUserId, vtUserName, vtPassword, vtLogdate;

        while (!spRecordset->adoEOF)
        {
            vtUserId 
= spRecordset->GetCollect("userid");
            vtUserName 
= spRecordset->GetCollect("username");
            vtPassword 
= spRecordset->GetCollect("password");
            
//vtLogdate = spRecordset->GetCollect("logdate");
            vtLogdate = spRecordset->Fields->GetItem(_variant_t(3L))->Value;
            //...
        }
    }
    
catch (_com_error &e)
    { hr 
= e.Error(); }
    
return hr;
}

注意:Command对象在Execute方法调用返回的Recordset对象,其GetRecordCount是无效的。
通过绑定记录集的访问方式

class UserRB: public CADORecordBinding
{
    BEGIN_ADO_BINDING(UserRB)
        ADO_FIXED_LENGTH_ENTRY(
1, adInteger, m_nUserID, m_ulIDStatus, TRUE)
        ADO_VARIABLE_LENGTH_ENTRY2(
2, adChar, m_szUserName,sizeof(m_szUserName), m_ulUserNameStatus,TRUE)
        ADO_VARIABLE_LENGTH_ENTRY2(
3, adChar, m_szPassword,sizeof(m_szPassword), m_ulPasswordStatus,TRUE)
        ADO_FIXED_LENGTH_ENTRY(
4, adDate, m_dtLogtime, m_ulLogtimeStatus, TRUE)
     END_ADO_BINDING()
public:
    
void dump(void)
    
{
        cout 
<< "userid:   " << ((m_ulIDStatus == adFldOK) ? m_nUserID : -1<< endl;
        cout 
<< "username: " << ((m_ulUserNameStatus == adFldOK) ? m_szUserName : "<NULL>"<< endl;
        cout 
<< "password: " << ((m_ulPasswordStatus == adFldOK) ? m_szPassword : "<NULL>"<< endl;
        CString strTime(
"<NULL>");
        
if (m_ulLogtimeStatus == adFldOK)
        
{
            SYSTEMTIME logdate;
            VariantTimeToSystemTime(m_dtLogtime, 
&logdate);
            CTime tm(logdate);
            strTime 
= tm.Format(_T("%m/%d/%Y %H:%M:%S"));
        }

        cout 
<< "logtime:  " << strTime << endl;       
    }


    UINT m_nUserID;
    ULONG m_ulIDStatus;

    CHAR m_szUserName[
1024];
    ULONG m_ulUserNameStatus;

    CHAR m_szPassword[
1024];
    ULONG m_ulPasswordStatus;

    DATE m_dtLogtime;
    ULONG m_ulLogtimeStatus;
}
;

HRESULT QueryAll_BR()
{
    HRESULT hr 
= S_OK;
    
try
    
{       
        _variant_t vtRecordsAffected;
        _RecordsetPtr spRecordset 
= NULL;

        GET_COMMAND
->CommandText = _T("spUserInfoQueryALL");
        GET_COMMAND
->CommandType = adCmdStoredProc;     
        GET_COMMAND
->ActiveConnection = GET_CONNECTION;
        spRecordset 
= GET_COMMAND->Execute(&vtRecordsAffected, NULL, adCmdStoredProc);
        spRecordset
->MoveFirst();
        UserRB urb;
        
{
            IADORecordBinding 
*pRB = NULL;
            TESTHR(spRecordset
->QueryInterface(__uuidof(IADORecordBinding), (LPVOID *)&pRB));
            pRB
->BindToRecordset(&urb);  
            pRB
->Release();
        }

        
while (!spRecordset->adoEOF)
        
{
            urb.dump();
            spRecordset
->MoveNext();
        }

        spRecordset
->Close();
    }

    
catch (_com_error &e)
    
{
        hr 
= e.Error();
    }


    
return hr;
}

抱歉!评论已关闭.