您现在的位置是: 软件 > 开发者网络 > 程序方舟 > 开发专栏 > Visual C++开发 > 正文
·速成电脑精英(包分配)白领高薪一族从这里开始



-Java套接字编程(下)
-MediaStudio Pro 6.5教程
-三款卸载软件最新试用
-基于Visual C++的Winsock API研究

在Visual C++中用ADO进行数据库编程
2002-03-20· ·wnchg 整理··VC知识库

上一页  1 2 3  

  5. 记录集的遍历、更新

      根据我们刚才通过执行SQL命令建立好的users表,它包含四个字段:ID,username,old,birthday
以下的代码实现:打开记录集,遍历所有记录,删除第一条记录,添加三条记录,移动光标到第二条记录,
更改其年龄,保存到数据库。
_variant_t vUsername,vBirthday,vID,vOld;
_RecordsetPtr m_pRecordset;
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM users",
  _variant_t((IDispatch*)m_pConnection,true),
  adOpenStatic,
  adLockOptimistic,
  adCmdText);
while(!m_pRecordset->adoEOF)
{
    vID = m_pRecordset->GetCollect(_variant_t((long)0));///取得第1列的值,从0开始计数,
    ///你也可以直接给出列的名称,如下一行
    vUsername = m_pRecordset->GetCollect("username");///取得username字段的值
    vOld = m_pRecordset->GetCollect("old");
    vBirthday = m_pRecordset->GetCollect("birthday");
    ///在DEBUG方式下的OUTPUT窗口输出记录集中的记录
    if(vID.vt != VT_NULL && vUsername.vt != VT_NULL && vOld.vt != VT_NULL && vBirthday.vt
!= VT_NULL) TRACE("id:%d,姓名:%s,年龄:%d,生日:%s\r\n", vID.lVal, (LPCTSTR)(_bstr_t)vUsername, vOld.lVal, (LPCTSTR)(_bstr_t)vBirthday); m_pRecordset->MoveNext();///移到下一条记录 } m_pRecordset->MoveFirst();///移到首条记录 m_pRecordset->Delete(adAffectCurrent);///删除当前记录 ///添加三条新记录并赋值 for(int i=0;i<3;i++) { m_pRecordset->AddNew();///添加新记录 m_pRecordset->PutCollect("ID",_variant_t((long)(i+10))); m_pRecordset->PutCollect("username",_variant_t("叶利钦")); m_pRecordset->PutCollect("old",_variant_t((long)71)); m_pRecordset->PutCollect("birthday",_variant_t("1930-3-15")); } m_pRecordset->Move(1,_variant_t((long)adBookmarkFirst));///从第一条记录往下移动一条记录,即移动
到第二条记录处 m_pRecordset->PutCollect(_variant_t("old"),_variant_t((long)45));///修改其年龄 m_pRecordset->Update();///保存到库中
备注:多次查询可把查询过程做成一个函数ExecuteSQL让m_pRecordset获得连接指针m_pConnection查询结果
void ExecuteSQL(_ConnectionPtr  m_pConnection, _RecordsetPtr  m_pRecordset,CString strSql)
{
    //执行Select 语句
    BSTR bstrSQL = strSql.AllocSysString();           
     try
     {
        m_pRecordset->Open(bstrSQL,(IDispatch*)m_pConnection,adOpenDynamic,adLockOptimistic,
adCmdText); //adOpenDynamic:动态 adLockOptimistic乐观封锁法 adCmdText:文本查询语句 } catch(_com_error error) { CString errorMessage; errorMessage.Format("%s",(LPTSTR)error.Description()); AfxMessageBox(errorMessage); } } //出错处理: 3127——没有找到目标表 3092——目标表已经存在 例如: catch(const _com_error e) { AfxMessageBox(e.Description()); long errorCode=e.WCode(); if(3127==errorCode) AfxMessageBox("表不存在"); if(3092==errorCode) AfxMessageBox("表已经存在"); return FALSE; }

上一页  1 2 3  

■ 相关内容
 为对话框程序添加工具条和状态栏
 在 ADO.NET 数据集中浏览多个相关表
 基于Visual C++ 的自动化客户端的实现
 通过COM技术实现Windows外壳编程
 用托管C++开发Win表单的一般方法
 Visual C++实现视频图像处理技术
 ADO程序员使用的ADO .NET
 在存储过程中调用外部的动态连接库
 细说ADO.NET命令
 在Visual C++中如何利用UDL文件来建立ADO连接
 Visual C++多线程DAO处理
 Visual C++ 系统及硬件编程
 用Visual C++开发数据库应用程序
 Visual C++ 网络与通讯
 Visual C++界面编程
 Visual C++中实现对图像数据的读取显示
 用Visual C++增强Notes打印功能
 利用Visual C++/MFC开发Windows程序
 Visual C++/MFC入门教程
 Visual C++6.0开发灰度位图处理
感谢 访问天极网,如果您觉得该文章涉及版权问题,请看这里!