码迷,mamicode.com
首页 > 数据库 > 详细

VS2013 MFC ODBC连接SQL SERVER数据库编程(二)

时间:2014-12-13 13:33:54      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   os   sp   for   strong   

VS2013 MFC ODBC连接SQL SERVER数据库编程(二)

转载请注明:http://blog.csdn.net/my_acm/article/category/2616577


在上一篇中,我已经说明了如何连接SQL SERVER数据库

下面参考http://wenku.baidu.com/link?url=h1rGgnhSmnclH2lFexHmlP_SZ2LUDRy5xM4wrbTfYlRXZLj2o0ugMT_PFGRxA1XI2pm8NUuEcKGQRA6D77ZieDoyMAqlUUDMaQQz2nqYTNm

做出了一部分实现,效果图如下所示,全部功能实现将会在下一篇中给出。

bubuko.com,布布扣

首先在连接数据库的基础上,做出一张如上图所示的界面,这个不难吧。

可以看到在连接数据库的score.h中,Cscore类继承了CRecordset类,有六个成员变量,分别对应数据库中的六个字段。

bubuko.com,布布扣



我们将编辑框,List control分别绑定变量,如下图所示。

bubuko.com,布布扣



然后在TestDlg.cpp中的OnInitDialog函数中添加如下代码。

bubuko.com,布布扣


添加成员函数void List_All()用于把表中数据全部都显示到list control中。

bubuko.com,布布扣

bubuko.com,布布扣


下面是list_all的具体实现。

void CTestDlg::List_All(CString str){
	m_list.DeleteAllItems();
	Cscore m_score;
	try{
		if (m_score.IsOpen())
			m_score.Close();
		if (!m_score.Open(CRecordset::snapshot, str)){
			MessageBox(L"打开数据库失败", L"数据库错误", MB_OK);
			return;
		}
	}
	catch (CDBException *e){
		e->ReportError();
	}
	
	int nindex = 0;
	m_score.MoveFirst();
	CString uscore, tscore, fscore;

	while (!m_score.IsEOF()){
		LV_ITEM litem;
		litem.mask = LVIF_TEXT;
		litem.iItem = nindex;
		litem.iSubItem = 0;
		litem.pszText = L"";
		
		m_list.InsertItem(&litem);
		
		m_list.SetItemText(nindex, 0, m_score.m_stuid);
		m_list.SetItemText(nindex, 1, m_score.m_stuname);
		m_list.SetItemText(nindex, 2, m_score.m_stuclass);

		uscore.Format(L"%3d", m_score.m_usualscore);
		tscore.Format(L"%3d", m_score.m_testscore);
		fscore.Format(L"%3d", m_score.m_totalscore);

		m_list.SetItemText(nindex, 3, uscore);
		m_list.SetItemText(nindex, 4, tscore);
		m_list.SetItemText(nindex, 5, fscore);

		m_score.MoveNext();
		nindex++;
	}
	m_score.Close();
}


VS2013 MFC ODBC连接SQL SERVER数据库编程(二)

标签:style   blog   http   io   ar   os   sp   for   strong   

原文地址:http://blog.csdn.net/my_acm/article/details/41908673

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!