ADO的连接

1、在stdafx.h文件最后(即#endif // _AFX_NO_AFXCMN_SUPPORT下面)添加: #import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF","adoEOF") 2、初始化COM: AfxOleInit();//这行代码要放在功能执行前,如果是基于对话框建立的程序,那就放在第一个对话框类的OnInitDialog()函数的return TRUE;前 3、在用到数据库的地方: _ConnectionPtr m_pConnection;///声明数据库连接变量 _RecordsetPtr m_pRecordset;///声明数据库集合变量 CString strCn; strCn.Empty(); (1)连接数据库 HRESULT hr; try { _variant_t RecordsAffected; hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象 if(SUCCEEDED(hr)) { hr = m_pConnection->Open("DSN=test;UID=;PWD=;","","",adModeUnknown);///连接数据库 } } catch( _com_error e)///捕捉异常 { CString errormessage; errormessage.Format("连接数据库失败!/r/n错误信息:%s",e.ErrorMessage()); AfxMessageBox(errormessage);///显示错误信息 } (2)通过SQL读数据 CString sql; try { m_pRecordset.CreateInstance("ADODB.Recordset"); m_pRecordset->Open((_variant_t)sql,_variant_t((IDispatch*)m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText); } catch(_com_error e)///捕捉异常 { CString errorMessage = e.ErrorMessage(); AfxMessageBox("读取数据时出错:"+sql+errorMessage);///显示错误信息 } (3)通过sql语句添加、修改、删除记录 _variant_t RecordsAffected; try { m_pConnection->Execute((_bstr_t)Sql,&RecordsAffected,adCmdText); } catch(_com_error *e) { AfxMessageBox(e->ErrorMessage()); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值