ADODB连接数据库,通过msado15组件实现,VC2005

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

//确定你有这个DLL

void show1();
int main(int argc, char* argv[])  
{    
 show1();
 return   0;  
}

void show1()
{
 CoInitialize(NULL);  
 HRESULT hr;
 try    
 {  
  //_ConnectionPtr pConn("ADODB.Connection");
  _ConnectionPtr pConn;
  hr = pConn.CreateInstance("ADODB.Connection");///创建Connection对象
  if(SUCCEEDED(hr))
  {
   _RecordsetPtr pRst("ADODB.Recordset");
   pConn->put_ConnectionTimeout(long(5));
   //pConn->Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb", "", "", adModeUnknown);
   pConn->Open("Provider=sqloledb;Data Source=.;Initial Catalog=CMS;User Id=sa;Password=sa;","","",adConnectUnspecified); //SqlServer2005

//可以通过设置连接不同的数据库,具体请参考MSDN

   pRst->Open("SELECT * FROM CMS",// 查询DemoTable表中所有字段
    pConn.GetInterfacePtr(),  // 获取库接库的IDispatch指针
    adOpenDynamic,
    adLockOptimistic,
    adCmdText);
   //pRst->Open("CMS",_variant_t((IDispatch *)pConn,true),adOpenStatic,adLockReadOnly,adCmdTable);
   int i=0;
   while(!pRst->adoEOF)
   {
    i++;
    printf("%d/n",i);
    //_variant_t var= pRst->GetCollect("cms_id");
    //_variant_t var= pRst->GetCollect(long(0));
    //if(var.vt != VT_NULL)
    // printf("%s/n",(char *)(LPCSTR)_bstr_t(var));
    printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
    pRst->MoveNext();
   }
 /*  pRst->MoveFirst();
   printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
   pRst->Move(3);
   printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));

   pRst->MoveLast();
   printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));*/

   //pRst->MovePrevious();
   //pRst->Delete(adAffectCurrent); 
   // 参数adAffectCurrent为删除当前记录
   //pRst->Update();

   _CommandPtr m_pCommand;
   m_pCommand.CreateInstance(__uuidof(Command));
   // 将库连接赋于它
   m_pCommand->ActiveConnection = pConn; 
   // SQL语句
   //m_pCommand->CommandText = "SELECT * FROM CMS";
   m_pCommand->CommandText = "update CMS set type_id=2 where cms_id=1";
   // 执行SQL语句,返回记录集
   pRst = m_pCommand->Execute(NULL, NULL,adCmdText);
   //while(!pRst->adoEOF)
   //{
   // i++;
   // printf("%d/n",i);
   // //_variant_t var= pRst->GetCollect("cms_id");
   // //_variant_t var= pRst->GetCollect(long(0));
   // //if(var.vt != VT_NULL)
   // // printf("%s/n",(char *)(LPCSTR)_bstr_t(var));
   // printf("cms_id and type_id '%s %s'/n",(char*)((_bstr_t)pRst->GetFields()->GetItem("cms_id")->GetValue()),(char*)((_bstr_t)pRst->Fields->Item["type_id"]->Value));
   // pRst->MoveNext();
   //}
   pRst->Close();
   if( pConn->State )
   pConn->Close(); 
  }
 }  
 catch(_com_error &e)  
 {  
  printf("Description= '%s'/n",(char*)e.Description());  
 }        
 ::CoUninitialize();  
}

猜你喜欢

转载自blog.csdn.net/CNCbird/article/details/5384760