1> 设置数据源,控制面板----管理工具------用户SDN----添加----添加所用的驱动-----数据源
名-----数据库选择-------找到设置好的数据库文件。例如:Access.mdb
ADO数据库:
ADO对象包括:连接对象Connection Object、命令对象Command Object、记录集对象 RecordSet Object、字段对象Field Object、记录对象Record Object、错误对象Error Object、参数对象Parameter Object、属性对象Property Object、流对象Stream Object 其中连接对象、命令对象、记录集对象是最重要的
ADO的引入:
需要在StdAfx.h文件的末尾加入下面的语句:#import “C:\\program files\\common files\\system\\ado\\msado15.dll” no_namespace rename(“EOF”,”adoEOF”)
同时在编译时系统会生成msado15.tlh,ado15.tli两个C++头文件来定义数据库
数据库连接: 定义连接对象
_ConnectionPtr m_pConnection; _RecordSetPtr m_pRecordset _CommandPtr m_pCommand
实例化:m_pConnection.CreateInstance(_uuidof(Connection)); M_pRecordSet.CreateInstance(_uuidof(Recordset)); M_pCommand.CreateInstance(ADODB.COMMAND); 连接数据库:try { _bstr_t strConnect = “driver = {SQL Server};Server = 127.0.0.1;DATABASE = workerdb;UID = Jack;PWD = Jack”; M_pConnection ->Open(strConnect,””,””,-1); } Catch(_com_error e) { MessageBox(NULL,e.ErrorMessage(),”Error”,MB_OK); }
查询记录:
可以通过Open方法来进行记录的查询,函数原型如下 HRESULT Recordset15::Open() Try { Cstring strSql = “select * from worder_information”; BSTR bstrSQL = strSql.AllocSysString(); M_pRecordset –>Open(bstrSQL,(Idispatch *)m_pConnection,adOpenDynamic,adLockOptimistic,adCmdText);
Int nID;
Cstring strName; Int nPay;
Int nIndex = 0; //遍历记录
While(!m_pRecordset ->adoEOF) { nIndex ++; _variant_t varValue; varValue = m_pRecordset ->Fields ->GetItem(“wordID”)->Value; if(varValue.vt != VT_NULL) { nID = varValue.iVal; } varValue = m_pRecordset ->GetCollect(“worder_name”); if(varValue.vt != VT_NULL) { strName = (char *)_bstr_t(varValue); } varValue = m_pRecordset ->Fields ->GetItem(“worder_pay”)->GetValue(); if(varValue.vt != VT_NULL) { nPay = varValue.iVal; }
Cstring strResult;
strResult.Format(“The record %d values:%d,%s,%d!”,i,nID,strName,nPay); AfxMessageBox(strResult); //移动到下一条记录
M_pRecordset->MoveNext; }
//读取完后,关闭记录集 M_pRecordset ->Close(); }
Catch(_com_error e) { MessageBox(NULL,e.ErrorMessage(),”Error”,MB_OK); }
添加记录:
1> 使用Connection Object的Execute方法 2> 使用Command Object的Executefangfa
3> 记录集提供的AddNew方法 Try {
Cstring strSql = “select * from worker_information”; BSTR bstrSQL = strSql.AllocSysString(); M_pRecordset -> Open(bstrSQL,(Idispatch *)m_pConnection,adOpenDynamic,adLockPessimistic,adCmdText);
//添加记录
M_pRecordset ->AddNew();
M_pRecordset ->Fields ->GetItem(“worker_ID”) ->Value = 3;
M_pRecordset ->Fields ->GetItem(“worder_Name”) ->Value = _bstr_t(“Hello”); M_pRecordset ->Fields ->GetItem(“worder_Pay”) ->Value = 2300;
//更新数据
M_pRecordset ->Update();
M_pRecordset ->Close(); }
Catch( _com_error e) { AfxMessageBox(e.ErrorMessage()); }
修改记录:
还是用第三种方法,但不需要AddNew() Try {
Cstring strSql = “select * from worker_information where worker_ID = 3”; BSTR bstrSQL = strSql.AllocSysString(); M_pRecordset -> Open(bstrSQL,(Idispatch *)m_pConnection,adOpenDynamic,adLockPessimistic,adCmdText); //修改
If(!m_pRecordset ->adEOF) { M_pRecordset ->Fields ->GetItem(“worker_ID”) ->Value = 3;
M_pRecordset ->Fields ->GetItem(“worder_Name”) ->Value = _bstr_t(“方格”); M_pRecordset ->Fields ->GetItem(“worder_Pay”) ->Value = 2500; //更新数据
M_pRecordset ->Update(); }
M_pRecordset ->Close(); }
Catch( _com_error e) { AfxMessageBox(e.ErrorMessage()); }
删除记录: Try {
_variant_t vNULL;
vNULL.vt = VT_ERROR; //定义为无参数
vNULL.scode = DISP_E_PARAMNOTFOUND;
//下面这条语句很关键,建立连接成功后将m_pConnection赋值给ActiveConnection M_pCommand->ActiveConnection = m_pConnection; //命令串
M_pCommand ->CommandText = “delete worder_information where worker_ID = 3”; //执行命令
M_pRecordset = m_pCommand - > Execute(&vNULL,&vNULL,adCmdText); }
Catch( _com_error e) { AfxMessageBox(e.ErrorMessage()); }
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务