279上位机VC MFC使用DAO综合查询记录
279上位机VC MFC使用DAO综合查询记录
功能展示
在用DAO进行编写数据库程序时,查询功能也是很常用的一种功能,我们当前例程使用CDaoTableDef类演示如何查询数据库,效果如图,可以通过<按评定级别查询><按年龄区查询>也可以<属性查询> 要点提示 1.在对记录集操作之后,最后退出程序,一定要把记录集指针重新放置在首位置,随后调用Close函数关闭该记录对象; 2. COleVariant类型的变量在转化为长整形或字符串类型时,要恰当的V_I4(COleVariant&VAR)函数,以免数据出现截断错误; 实现功能 1.新建基于单文档应用程序,在创建过程中选择基于DAO,数据库文件选择例程根目录数据库文件,表选择course,具体的创建操作过程可以参考视频演示; 2.根据例程界面添加两组合框控件ID为IDC_COMBO1, IDC_COMBO2,再添加一编辑框控件ID为IDC_AGEEDIT 3,添加两列表框控件ID分别 为IDC_ITEMLIST, IDC_FEATURELIST用于显示搜索的结果;
4.添加三按钮<按评定级别查询><按年龄区查询><属性查询>用于执行查询操作,关联点击函数 - void CGkbc8View::OnComreq() //按评定级别查询
- {
- CString sCombo1Text;
- CComboBox *pComboBox1 = (CComboBox *)GetDlgItem(IDC_COMBO1);
- pComboBox1->GetLBText(pComboBox1->GetCurSel(),sCombo1Text);
- CListCtrl *pItemList = (CListCtrl *)GetDlgItem(IDC_ITEMLIST);
- pItemList->DeleteAllItems();
- CGkbc8Set DaoRecordset;
- CString str;
- str.Format("%s",DaoRecordset.GetDefaultDBName());
- CDaoDatabase db;
- db.Open(str);
- CDaoQueryDef DaoQueryDef(&db);
- CString sql;
- sql.Format("Select * from course WHERE comment='%s'",sCombo1Text);
- DaoQueryDef.Create(NULL,sql);
- CDaoRecordset rs(&db);
- rs.Open(&DaoQueryDef);
复制代码- COleVariant var;
- CString str1;
- int nItem;
- rs.MoveFirst();
- while(!rs.IsEOF())
- {
- var=rs.GetFieldValue(0);
- str1.Format(_T("%ld"),V_I4(&var));
- nItem=pItemList->InsertItem(0,str1);
- var=rs.GetFieldValue(1);
- str1.Format(_T("%s"),V_I4(&var));
- pItemList->SetItemText(nItem,1,str1);
- var=rs.GetFieldValue(2);
- str1.Format(_T("%ld"),V_I4(&var));
- pItemList->SetItemText(nItem,2,str1);
- var=rs.GetFieldValue(3);
- str1.Format(_T("%s"),V_I4(&var));
- pItemList->SetItemText(nItem,3,str1);
- var=rs.GetFieldValue(4);
- str1.Format(_T("%s"),V_I4(&var));
- pItemList->SetItemText(nItem,4,str1);
- rs.MoveNext();
- }
- rs.MoveFirst();
- rs.Close();
- db.Close();
- }
- void CGkbc8View::OnAgereq() //按年龄区查询
- {
- long nAge = (long)GetDlgItemInt(IDC_AGEEDIT);
- CString sCombo2Text;
- CComboBox *pComboBox2 = (CComboBox *)GetDlgItem(IDC_COMBO2);
- pComboBox2->GetLBText(pComboBox2->GetCurSel(),sCombo2Text);
- //
- CListCtrl *pItemList = (CListCtrl *)GetDlgItem(IDC_ITEMLIST);
- pItemList->DeleteAllItems();
- CDaoDatabase db;
- CGkbc8Set DaoRecordset;
- CString str;
- str.Format("%s",DaoRecordset.GetDefaultDBName());
- db.Open(str);
- CDaoQueryDef DaoQueryDef(&db);
- CString sql;
复制代码- if(sCombo2Text=="大于")
- sql.Format("Select * from course WHERE age>%d",nAge);
- else if(sCombo2Text=="小于")
- sql.Format("Select * from course WHERE age<%d",nAge);
- else
- sql.Format("Select * from course WHERE age=%d",nAge);
- DaoQueryDef.Create(NULL,sql);
- CDaoRecordset rs(&db);
- rs.Open(&DaoQueryDef);
- COleVariant var;
- CString str1;
- int nItem;
- rs.MoveFirst();
- while(!rs.IsEOF())
- {
- var=rs.GetFieldValue(0);
- str1.Format(_T("%ld"),V_I4(&var));
- nItem=pItemList->InsertItem(0,str1);
- var=rs.GetFieldValue(1);
- str1.Format(_T("%s"),V_I4(&var));
- pItemList->SetItemText(nItem,1,str1);
- var=rs.GetFieldValue(2);
- str1.Format(_T("%ld"),V_I4(&var));
- pItemList->SetItemText(nItem,2,str1);
- var=rs.GetFieldValue(3);
- str1.Format(_T("%s"),V_I4(&var));
- pItemList->SetItemText(nItem,3,str1);
- var=rs.GetFieldValue(4);
- str1.Format(_T("%s"),V_I4(&var));
- pItemList->SetItemText(nItem,4,str1);
- rs.MoveNext();
- }
- rs.MoveFirst();
- rs.Close();
- db.Close();
- }
- void CGkbc8View::OnFeabutton() //属性查询
- {
- CListCtrl *pList = (CListCtrl *)GetDlgItem(IDC_FEATURELIST);
- pList->DeleteAllItems();
- //
- CDaoDatabase db;
复制代码- CGkbc8Set DaoRecordset;
- CString str;
- str.Format("%s",DaoRecordset.GetDefaultDBName());
- db.Open(str);
- CDaoQueryDef DaoQueryDef(&db);
- CString sql="Select * from course";
- DaoQueryDef.Create(NULL,sql);
- int num=(int)DaoQueryDef.GetFieldCount();
- CDaoFieldInfo fi;
- CString str1;
- CString str2;
- CString str3;
- for(int i=0;i<num;i++)
- {
- DaoQueryDef.GetFieldInfo(i,fi,AFX_DAO_PRIMARY_INFO);
- str1=fi.m_strName;
- str2.Format("%d",fi.m_nType);
- str3.Format("%d",fi.m_lSize);
- int n=pList->InsertItem(0,str1);
- pList->SetItemText(n,1,str2);
- pList->SetItemText(n,2,str3);
- }
- db.Close();
- }
复制代码5.记得在程序初始化时初始化列表控件,及组合框数据
我们来演示整个功能实现过程
|