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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值