使用ado及listctrl控件操作access数据库

使用ado及listctrl控件操作access数据库

ID:34739660

大小:188.18 KB

页数:13页

时间:2019-03-10

使用ado及listctrl控件操作access数据库_第1页
使用ado及listctrl控件操作access数据库_第2页
使用ado及listctrl控件操作access数据库_第3页
使用ado及listctrl控件操作access数据库_第4页
使用ado及listctrl控件操作access数据库_第5页
资源描述:

《使用ado及listctrl控件操作access数据库》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库

1、使用ADO及listCtrl控件操作ACCESS数据库第1步:显示数据记录●创建基于对话框的应用程序AdoList。●加入ListControl控件,设置属性如下:ID值为IDC_LIST1View列表选ReportSingleselection选中(限制列表项单选)●在StdAfx.h文件加入下面命令:#import"C:ProgramFilesCommonFilesSystemadomsado15.dll"no_namespacerename("EOF","adoEOF")●使用类向导为IDC_LIST1列表视控件绑定控件型变量m_

2、list●在CadoListDlg类添加下面三个成员变量_ConnectionPtrm_pConnection;_RecordsetPtrm_pRecordset;●在BOOLCAdoListDlg::OnInitDialog()窗口初始化函数中加入下面命令://TODO:AddextrainitializationhereCoInitialize(NULL);try{HRESULThr;m_pConnection.CreateInstance("ADODB.Connection");hr=m_pConnection->Open("Provide

3、r=Microsoft.Jet.OLEDB.4.0;DataSource=stu1.mdb","","",adModeUnknown);}catch(_com_errore){CStringerrormessage;errormessage.Format("连接数据库失败!错误信息:%s",e.ErrorMessage());AfxMessageBox(errormessage);}try{m_pRecordset.CreateInstance(__uuidof(Recordset));m_pRecordset->Open("SELECT*F

4、ROMstudent",m_pConnection.GetInterfacePtr(),adOpenDynamic,adLockOptimistic,adCmdText);}catch(_com_errore){CStringerrormessage;errormessage.Format("连接数据库表失败!错误信息:%s",e.ErrorMessage());AfxMessageBox(errormessage);}m_list.SetExtendedStyle(LVS_EX_FLATSB

5、LVS_EX_FULLROWSELECT//点击

6、一项时,整行都为选中状态

7、LVS_EX_GRIDLINES);//设置网格线m_list.InsertColumn(0,"学号",LVCFMT_LEFT,80);m_list.InsertColumn(1,"姓名",LVCFMT_LEFT,50);m_list.InsertColumn(2,"年龄",LVCFMT_RIGHT,36);disp();//显示数据库数据记录returnTRUE;//returnTRUEunlessyousetthefocustoacontrol●在CAdoListDlg类添加下面成员函数disp()(并在类定义中声明

8、)显示数据库记录:voidCAdoListDlg::disp(){m_list.DeleteAllItems();inti=0;m_pRecordset->MoveFirst();while(!m_pRecordset->adoEOF)//adoEOF是末尾标识{_variant_ttheValue;CStringid,name,age;theValue=m_pRecordset->GetCollect("StuID");//获取学号if(theValue.vt!=VT_NULL)id=(char*)_bstr_t(theValue);theVa

9、lue=m_pRecordset->GetCollect("StuName");//获取姓名if(theValue.vt!=VT_NULL)name=(char*)_bstr_t(theValue);theValue=m_pRecordset->GetCollect("age");//获取年龄if(theValue.vt!=VT_NULL)age.Format("%d",theValue.iVal);m_list.InsertItem(i,"");m_list.SetItemText(i,0,id);m_list.SetItemText(i,1,

10、name);m_list.SetItemText(i,2,age);m_pRecordset->MoveNext();//移动到下一行(

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。