ado 使用

来源:互联网 发布:苹果看视频软件 编辑:程序博客网 时间:2024/04/28 21:34

1.导入ADO链接库

 //stdafx.h#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF

2.初始化com库

if(!AfxOleInit()) //LOG

3.创建一个数据库连接对象

_ConnectionPtr      m_pConnection;try  {       m_pConnection.CreateInstance(__uuidof(Connection));       m_pConnection->Open((_bstr_t)"DSN=dbsource;Uid=sa;Pwd=800100","","",adModeUnknown);  } catch(_com_error _e){  CString _err= (LPCTSTR)(_bstr_t)_e.Description();}//lastpConnection->Close();

4.1 sql 命令

_CommandPtr pCommand;      pCommand.CreateInstance(__uuidof(Command)); pCommand->ActiveConnection=pConn; pCommand->CommandText="INSERT like"; pCommand->CommandType=adCmdText; pCommand->Parameters->Refresh();    pCommand->Execute(NULL,NULL,adCmdUnknown);   

4.2 记录集

 _RecordsetPtr m_pRecordset; try   {      //m_pRecordset=m_pConnection->Execute(_T("select *  from"),NULL,adCmdText);m_pRecordset.CreateInstance(__uuidof(Recordset));  m_pRecordset->Open(_bstr_t(“select from...”), m_pConnection.GetInterfacePtr(),adOpenDynamic, adLockOptimistic, adCmdText; } catch(_com_error _e) {  CString _err= (LPCTSTR)(_bstr_t)_e.Description(); }

//read

m_pRecordset->MoveFirst();while (m_pRecordset->adoEOF)  {          //like//pRecordset->getCollect(“COLUMN_NAME”);//pRecordset->getCollect(index); char* _name=(char*)(_bstr_t)m_ado.m_pRecordset->GetCollect("id"));  m_pRecordset->MoveNext();  
} 
//add record
m_pRecordset->AddNew();  m_pRecordset->PutCollect("name ",(_bstr_t)"fly");  m_pRecordset->Update();
</pre><span style="margin-top:0px; margin-right:0px; margin-bottom:0px; margin-left:0px; padding-top:0px; padding-right:0px; padding-bottom:0px; padding-left:0px; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:none; border-width:initial; border-color:initial; background-color:inherit"><span style="color:#333333"></span></span></p><p></p><div class="alt" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:3px!important; padding-bottom:0px!important; padding-left:10px!important; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:solid; border-width:initial; border-color:initial; list-style-type:decimal-leading-zero; list-style-position:outside!important; border-left-color:rgb(108,226,108); line-height:18px">//update</div><div class="alt" style="margin-top:0px!important; margin-right:0px!important; margin-bottom:0px!important; margin-left:0px!important; padding-top:0px!important; padding-right:3px!important; padding-bottom:0px!important; padding-left:10px!important; border-style:initial; border-color:initial; border-top-style:none; border-right-style:none; border-bottom-style:none; border-left-style:solid; border-width:initial; border-color:initial; list-style-type:decimal-leading-zero; list-style-position:outside!important; border-left-color:rgb(108,226,108); line-height:18px"><pre class="cpp" name="code">m_pRecordset->Move((long)pos);  m_pRecordset->PutCollect("id",(_bstr_t)m_id); m_pRecordset->Delete(adAffectCurrent); 

//delete
m_pRecordset->Move(pos, vtMissing);  m_pRecordset->Delete(adAffectCurrent); 
//last

m_pRecordset->Close();

4.3存储过程

_CommandPtr   cmmd;   HRESULT   hr   =   cmmd.CreateInstance(__uuidof(Command));  if(FAILED(hr))         {            printf("NewNetDatabase()中创建_CommandPtr对象失败");                 }   _ParameterPtr   param;    param=cmmd->CreateParameter("ReturnValue",adInteger,   adParamReturnValue, sizeof(int),SqlReturn);        cmmd->Parameters->Append(param);      param=cmmd->CreateParameter("TaskID",adInteger,   adParamInput, sizeof(int),_currTask->m_TaskID);        cmmd->Parameters->Append(param);         param   =   cmmd->CreateParameter("ItenID",adInteger,adParamOutput,sizeof(int),ItemId);        cmmd->Parameters->Append(param);         param   =   cmmd->CreateParameter("TeNum",adVarChar,adParamOutput,50,_variant_t(Number.c_str()));  cmmd->Parameters->Append(param);         cmmd->CommandText=_bstr_t("CTI_GetATaskItem");//存储过程的名称         cmmd->ActiveConnection   =  m_pConnection;//需要使用的ADO连接         cmmd->CommandType=adCmdStoredProc;    try  {      cmmd->Execute(NULL,   NULL,   NULL);    SqlReturn=(int)cmmd->Parameters->GetItem("ReturnValue")->GetValue();//通过参数返回值    if(SqlReturn == 1)   {    ItemId=(int)(cmmd->Parameters->GetItem("ItenID")->GetValue());//通过参数返回值      Number=(LPCSTR)_bstr_t(cmmd->Parameters->GetItem("TeNum")->GetValue());;//通过参数返回值    }    }  catch (_com_error e)  {   printf("%s",e.Description());   cmmd.Detach();     break;  }  cmmd.Detach();     


0 0