【 tulaoshi.com - 编程语言 】
                             
                            预备:(1)、引入ADO类 #import "c:program filescommon filessystemadomsado15.dll"   
  no_namespace   
  rename ("EOF", "adoEOF")  
  (2)、初始化COM在MFC中可以用AfxOleInit();非MFC环境中用:   
  CoInitialize(NULL);  
  CoUnInitialize();(3)#import 包含后就可以用3个智能指针了:_ConnectionPtr、_RecordsetPtr和_CommandPtr
1.连接和关闭数据库 (1)连接 例子:连接Access数据库  
  AfxOleInit();//初始化  
  HRESULT hr;  
  try  
  {  
  hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象  
  if(SUCCEEDED(hr))  
  {  
  m_pConnection-ConnectionTimeout = 0;  
  hr = m_pConnection-Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb", "", "", adModeUnknown);  
  //m_pConnection-PutDefaultDatabase ((_bstr_t)"DB");//设置默认数据库m_pCommand.CreateInstance(__uuidof(Command));  
  m_pCommand-CommandTimeout = 5;  
  m_pCommand-ActiveConnection = m_pConnection;  
  }  
  }  
  catch(_com_error e)///捕捉异常  
  {  
  CString errormessage;  
  errormessage.Format("连接数据库失败!错误信息:%s",e.ErrorMessage());  
  AfxMessageBox(errormessage);///显示错误信息  
  }
(2)、关闭 //假如数据库连接有效  
  if( m_pConnection-State )  
  m_pConnection-Close();  
  m_pConnection = NULL;  (3)、设置连接时间 //设置连接时间-----------------------------------   
  pConnection-put_ConnectionTimeout(long(5));  
  2.打开一个结果集(1)打开,首先创建一个_RecordsetPtr实例,然后调用Open()得到一条SQL语句的执行结果  
  _RecordsetPtrm_pRecordset;  
  m_pRecordset.CreateInstance(__uuidof(Recordset));// 在ADO操作中建议语句中要常用try...catch()来捕捉错误信息,  
  // 因为它有时会经常出现一些意想不到的错误。jingzhou xu  
  try  
  {  
  m_pRecordset-Open("SELECT * FROM DemoTable",// 查询DemoTable表中所有字段  
  m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针  
  adOpenDynamic,  
  adLockOptimistic,  
  adCmdText);  
  }  
  catch(_com_error *e)  
  {  
  AfxMessageBox(e-ErrorMessage());  
  }(2)关闭结果集 m_pRecordset-Close();
3.操作一个结果集(1)、遍历(读取)  
  a)、用pRecordset-adoEOF来判定数据库指针是否已经移到结果集的末尾了;m_pRecordset-BOF判定是否 在第一条记录前面: while(!m_pRecordset-adoEOF)  
  {  
  var = m_pRecordset-GetCollect("Name");  
  if(var.vt != VT_NULL)  
  strName = (LPCSTR)_bstr_t(var);  
  var = m_pRecordset-GetCollect("Age");  
  if(var.vt != VT_NULL)  
  strAge = (LPCSTR)_bstr_t(var);  
  m_AccessList.AddString( strName + " -- "+strAge );  
  m_pRecordset-MoveNext();  
  }
     b)、取得一个字段的值的办法有两种办法一是//表示取得第0个字段的值 m_pRecordset-GetCollect("Name");或者 m_pRecordset-GetCollect(_variant_t(long(0));二是  
  pRecordset-get_Collect("COLUMN_NAME");或者 pRecordset-get_Collect(long(index));(2)、添加a)、调用m_pRecordset-AddNew();  
  b)、调用m_pRecordset-PutCollect();给每个字段赋值  
  c)、调用m_pRecordset-Update();确认(3)、修改  
  (4)、删除  
  a)、把记录指针移动到要删除的记录上,然后调用Delete(adAffectCurrent) try  
  {  
  // 假设删除第二条记录  
  m_pRecordset-MoveFirst();  
  m_pRecordset-Move(1);          
  // 从0开始  
  m_pRecordset-Delete(adAffectCurrent);    
  // 参数adAffectCurrent为删除当前记录  
  m_pRecordset-Update();  
  }  
  catch(_com_error *e)  
  {  
  AfxMessageBox(e-ErrorMessage());  
  }  
预备:(1)、引入ADO类 #import "c:program filescommon filessystemadomsado15.dll"   
  no_namespace   
  rename ("EOF", "adoEOF")  
  (2)、初始化COM在MFC中可以用AfxOleInit();非MFC环境中用:   
  CoInitialize(NULL);  
  CoUnInitialize();(3)#import 包含后就可以用3个智能指针了:_ConnectionPtr、_RecordsetPtr和_CommandPtr
1.连接和关闭数据库 (1)连接 例子:连接Access数据库  
  AfxOleInit();//初始化  
  HRESULT hr;  
  try  
  {  
  hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象  
  if(SUCCEEDED(hr))  
  {  
  m_pConnection-ConnectionTimeout = 0;  
  hr = m_pConnection-Open( "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=db.mdb", "", "", adModeUnknown);  
  //m_pConnection-PutDefaultDatabase ((_bstr_t)"DB");//设置默认数据库m_pCommand.CreateInstance(__uuidof(Command));  
  m_pCommand-CommandTimeout = 5;  
  m_pCommand-ActiveConnection = m_pConnection;  
  }  
  }  
  catch(_com_error e)///捕捉异常  
  {  
  CString errormessage;  
  errormessage.Format("连接数据库失败!错误信息:%s",e.ErrorMessage());  
  AfxMessageBox(errormessage);///显示错误信息  
  }
(2)、关闭 //假如数据库连接有效  
  if( m_pConnection-State )  
  m_pConnection-Close();  
  m_pConnection = NULL;  (3)、设置连接时间 //设置连接时间-----------------------------------   
  pConnection-put_ConnectionTimeout(long(5));  
  2.打开一个结果集(1)打开,首先创建一个_RecordsetPtr实例,然后调用Open()得到一条SQL语句的执行结果  
  _RecordsetPtrm_pRecordset;  
  m_pRecordset.CreateInstance(__uuidof(Recordset));// 在ADO操作中建议语句中要常用try...catch()来捕捉错误信息,  
  // 因为它有时会经常出现一些意想不到的错误。jingzhou xu  
  try  
  {  
  m_pRecordset-Open("SELECT * FROM DemoTable",// 查询DemoTable表中所有字段  
  m_pConnection.GetInterfacePtr(),  // 获取库接库的IDispatch指针  
  adOpenDynamic,  
  adLockOptimistic,  
  adCmdText);  
  }  
  catch(_com_error *e)  
  {  
  AfxMessageBox(e-ErrorMessage());  
  }
     (2)关闭结果集 m_pRecordset-Close();
3.操作一个结果集(1)、遍历(读取)  
  a)、用pRecordset-adoEOF来判定数据库指针是否已经移到结果集的末尾了;m_pRecordset-BOF判定是否 在第一条记录前面: while(!m_pRecordset-adoEOF)  
  {  
  var = m_pRecordset-GetCollect("Name");  
  if(var.vt != VT_NULL)  
  strName = (LPCSTR)_bstr_t(var);  
  var = m_pRecordset-GetCollect("Age");  
  if(var.vt != VT_NULL)  
  strAge = (LPCSTR)_bstr_t(var);  
  m_AccessList.AddString( strName + " -- "+strAge );  
  m_pRecordset-MoveNext();  
  }b)、取得一个字段的值的办法有两种办法一是//表示取得第0个字段的值 m_pRecordset-GetCollect("Name");或者 m_pRecordset-GetCollect(_variant_t(long(0));二是  
  pRecordset-get_Collect("COLUMN_NAME");或者 pRecordset-get_Collect(long(index));(2)、添加a)、调用m_pRecordset-AddNew();  
  b)、调用m_pRecordset-PutCollect();给每个字段赋值  
  c)、调用m_pRecordset-Update();确认(3)、修改  
  (4)、删除  
  a)、把记录指针移动到要删除的记录上,然后调用Delete(adAffectCurrent) try  
  {  
  // 假设删除第二条记录  
  m_pRecordset-MoveFirst();  
  m_pRecordset-Move(1);          
  // 从0开始  
  m_pRecordset-Delete(adAffectCurrent);    
  // 参数adAffectCurrent为删除当前记录  
  m_pRecordset-Update();  
  }  
  catch(_com_error *e)  
  {  
  AfxMessageBox(e-ErrorMessage());  
  }