前言:
转载请附上连接,本帖原创请勿照抄。
使用VC++,MFC实现了连接MySQL实现分页功能的讲解以及实现。
项目名称MFC_Ado_Test
打开MFC界面新建四个按钮和一个ListControl控件,ListControl添加控件变量为m_list,更改ListControl控件属性。为上一页、下一页两个but添加控件变量为m_but1、m_but2。
stdafx里面添加链接数据库字符
#include // MFC 核心组件和标准组件
#include // MFC 扩展
#include // MFC 自动化类
#import "C:\Program Files\Common Files\System\ado\msado15.dll" no_namespace rename("EOF","adoEOF")rename("BOF","adoBOF")
#ifndef _AFX_NO_OLE_SUPPORT
#include // MFC 对 Internet Explorer 4 公共控件的支持
#endif
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include // MFC 对 Windows 公共控件的支持
#endif // _AFX_NO_AFXCMN_SUPPORT
#include // 功能区和控件条的 MFC 支持
.h文件编写初始化变量定义函数
public:
CListCtrl m_list;
CButton m_but1;
CButton m_but2;
afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
afx_msg void OnBnClickedButton3();
afx_msg void OnBnClickedButton4();
afx_msg void OnLvnItemchangedList2(NMHDR *pNMHDR, LRESULT *pResult);
_ConnectionPtr m_pCon;
_RecordsetPtr m_pRec;
_CommandPtr m_pCmd;
//strCmd 连接字符串 UserName 账号 PassWord密码
CString strCmd = "", UserName = "", PassWord = "";
//IntFind 分页变量 TotalNumber 表内字段总数
int IntFind = 0, TotalNumber = 0;
CString strTest = "";
//表内有多少条数据
int Number();
//测试
int ADOTest(int IntF);
//上一页
int LimitLift(int IntL);
//下一页
int LimitRight(int IntR);
//关表
int CloseSheet();
//按钮开关
int OnOff();
//初始化
int LoadUser();
//连接数据库
int OpenDatabase();
//尾
BOOL adoEOF() { return m_pRec->adoEOF; }
//向下移动一条记录
void MoveNext() { m_pRec->MoveNext(); }
};
.cpp文件编写初始化变量定义函数 (intList分页每页行数 int intList = 3)
OnInitDialog初始化控件显示内容(SQL语句在最下面)
// TODO: 在此添加额外的初始化代码
ListView_SetExtendedListViewStyleEx(m_list.m_hWnd, 0, LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
CString title[5];
title[0] = "AnimalID";
title[1] = "Cat";
title[2] = "Dog";
title[3] = "Snake";
title[4] = "Rabbit";
m_list.InsertColumn(0, title[0], LVCFMT_LEFT, 50);
m_list.InsertColumn(1, title[1], LVCFMT_LEFT, 50);
m_list.InsertColumn(2, title[2], LVCFMT_LEFT, 50);
m_list.InsertColumn(4, title[1], LVCFMT_LEFT, 50);
m_list.InsertColumn(5, title[2], LVCFMT_LEFT, 50);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
But1~4
void CMFCAdoTestDlg::OnBnClickedButton1()
{
LimitLift(IntFind - intList);
return;
}
void CMFCAdoTestDlg::OnBnClickedButton2()
{
LimitRight(IntFind + intList);
return;
}
void CMFCAdoTestDlg::OnBnClickedButton3()
{
OpenDatabase();
Number();
LoadUser();
OnOff();
return;
}
void CMFCAdoTestDlg::OnBnClickedButton4()
{
//关闭库连接结束
try
{
m_pCon->Close();
}
catch (...)
{
AfxMessageBox("CloseSheet-Error");
}
OnOK();
}
初始化数据库连接和关表
int CMFCAdoTestDlg::OpenDatabase() {
UserName = "root";
PassWord = "输入自己的密码";
strCmd.Format("DSN=MYSQL_ADO;Server=%s;Database=%s;UID=%s;PWD=%s", "127.0.0.1", "自己的数据库", UserName, PassWord);
m_pCon.CreateInstance(__uuidof(Connection));
m_pRec.CreateInstance(__uuidof(Recordset));
try
{
m_pCon->Open((LPCTSTR)strCmd, (LPCTSTR)UserName, (LPCTSTR)PassWord, adModeUnknown);
}
catch (_com_error &e)
{
AfxMessageBox("连接数据库失败");
return 0;
}
}
int CMFCAdoTestDlg::CloseSheet() {
try {
m_pRec->Close();
}
catch (...) {
AfxMessageBox("CloseSheet-Error");
}
return 0;
}
始化控件内容和分页实现
int CMFCAdoTestDlg::LoadUser() {
UpdateData(TRUE);
m_list.DeleteAllItems();
strTest.Format("%d", intList);
strCmd.Format("SELECT * FROM animal ORDER BY AnimalID LIMIT 0, 3");
strTest = "";
try
{
m_pRec->Open((LPCTSTR)strCmd, m_pCon.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
CString msg;
msg.Format("打开表失败");
AfxMessageBox(msg);
return 0;
}
if (NULL == m_pRec)
{
AfxMessageBox("查询数据出现错误!");
return 0;
}
if (m_pRec->adoEOF)
{
m_pRec->Close();
AfxMessageBox("Thereis no records in this table");
return 0;
}
_variant_t vAnimalID, vCat, vDog, vSnake, vRabbit;
CString StrList[20][5]; int i = 0, j = 0;
while (!m_pRec->adoEOF) {
//pRst->MoveFirst();//记录集指针移动到查询结果集的前面
vAnimalID = m_pRec->GetCollect(_variant_t("AnimalID"));
vCat = m_pRec->GetCollect(_variant_t("Cat"));
vDog = m_pRec->GetCollect(_variant_t("Dog"));
vSnake = m_pRec->GetCollect(_variant_t("Snake"));
vRabbit = m_pRec->GetCollect(_variant_t("Rabbit"));
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vAnimalID;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vCat;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vDog;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vSnake;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vRabbit;
m_list.InsertItem(i, StrList[i][0]);
m_list.SetItemText(i, 1, StrList[i][1]);
m_list.SetItemText(i, 2, StrList[i][2]);
m_list.SetItemText(i, 3, StrList[i][3]);
m_list.SetItemText(i, 4, StrList[i][4]);
i++;
j = 0;
m_pRec->MoveNext();//下一条记录
}
CloseSheet();
return 1;
}
int CMFCAdoTestDlg::LimitLift(int IntL) {
UpdateData(TRUE);
m_list.DeleteAllItems();
//"SELECT * FROM animal ORDER BY AnimalID DESC LIMIT %d,3", IntL 倒叙
strCmd.Format("SELECT * FROM animal ORDER BY AnimalID LIMIT %d,3", IntL);
try
{
m_pRec->Open((LPCTSTR)strCmd, m_pCon.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
AfxMessageBox("打开表失败");
return 0;
}
if (NULL == m_pRec)
{
AfxMessageBox("查询数据出现错误!");
return 0;
}
if (m_pRec->adoEOF)
{
m_pRec->Close();
AfxMessageBox("Thereis no records in this table");
return 0;
}
_variant_t vAnimalID, vCat, vDog, vSnake, vRabbit;
CString StrList[20][5]; int i = 0, j = 0;
while (!m_pRec->adoEOF) {
//pRst->MoveFirst();//记录集指针移动到查询结果集的前面
vAnimalID = m_pRec->GetCollect(_variant_t("AnimalID"));
vCat = m_pRec->GetCollect(_variant_t("Cat"));
vDog = m_pRec->GetCollect(_variant_t("Dog"));
vSnake = m_pRec->GetCollect(_variant_t("Snake"));
vRabbit = m_pRec->GetCollect(_variant_t("Rabbit"));
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vAnimalID;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vCat;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vDog;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vSnake;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vRabbit;
m_list.InsertItem(i, StrList[i][0]);
m_list.SetItemText(i, 1, StrList[i][1]);
m_list.SetItemText(i, 2, StrList[i][2]);
m_list.SetItemText(i, 3, StrList[i][3]);
m_list.SetItemText(i, 4, StrList[i][4]);
i++;
j = 0;
m_pRec->MoveNext();//下一条记录
}
IntFind=IntFind - 3;
OnOff();
CloseSheet();
return 1;
}
int CMFCAdoTestDlg::LimitRight(int IntR) {
UpdateData(TRUE);
m_list.DeleteAllItems();
strCmd.Format("SELECT * FROM animal ORDER BY AnimalID LIMIT %d, 3", IntR);
try
{
m_pRec->Open((LPCTSTR)strCmd, m_pCon.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
AfxMessageBox("打开表失败");
return 0;
}
if (NULL == m_pRec)
{
AfxMessageBox("查询数据出现错误!");
return 0;
}
if (m_pRec->adoEOF)
{
m_pRec->Close();
AfxMessageBox("Thereis no records in this table");
return 0;
}
_variant_t vAnimalID, vCat, vDog, vSnake, vRabbit;
CString StrList[20][5]; int i = 0, j = 0;
while (!m_pRec->adoEOF) {
vAnimalID = m_pRec->GetCollect(_variant_t("AnimalID"));
vCat = m_pRec->GetCollect(_variant_t("Cat"));
vDog = m_pRec->GetCollect(_variant_t("Dog"));
vSnake = m_pRec->GetCollect(_variant_t("Snake"));
vRabbit = m_pRec->GetCollect(_variant_t("Rabbit"));
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vAnimalID;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vCat;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vDog;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vSnake;
StrList[i][j++] = (LPSTR)(LPCSTR)(_bstr_t)vRabbit;
m_list.InsertItem(i, StrList[i][0]);
m_list.SetItemText(i, 1, StrList[i][1]);
m_list.SetItemText(i, 2, StrList[i][2]);
m_list.SetItemText(i, 3, StrList[i][3]);
m_list.SetItemText(i, 4, StrList[i][4]);
i++;
j = 0;
m_pRec->MoveNext();//下一条记录
}
IntFind=IntFind + 3;
OnOff();
CloseSheet();
return 1;
}
查询出该表的总记录数以及控制按钮开关
int CMFCAdoTestDlg::Number() {
strCmd.Format("SELECT COUNT(*) FROM animal");
try
{
m_pRec->Open((LPCTSTR)strCmd, m_pCon.GetInterfacePtr(), adOpenDynamic, adLockOptimistic, adCmdText);
}
catch (_com_error e)
{
AfxMessageBox("获取表总数失败");
return 0;
}
_variant_t COUNT;
CString str = "";
COUNT = m_pRec->GetCollect(_variant_t("COUNT(*)"));
str = (LPSTR)(LPCSTR)(_bstr_t)COUNT;
TotalNumber = (_ttoi(str))-3;
CloseSheet();
return 1;
}
int CMFCAdoTestDlg::OnOff() {
if (IntFind == 0)
{
m_but1.EnableWindow(FALSE);//禁用按钮
}
else
{
m_but1.EnableWindow(true);
}
if (IntFind == TotalNumber)
{
m_but2.EnableWindow(FALSE);//禁用按钮
}
else
{
m_but2.EnableWindow(true);
}
return 1;
}
SQL建表以及添加数据
create table animal
(
AnimalID int,
Cat VARCHAR2(50),
Dog VARCHAR2(50),
Snake VARCHAR2(50),
Rabbit VARCHAR2(50)
)
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(1, '蓝猫', '小狗', '小蛇','小兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(2, '大猫', '大狗', '大蛇','大兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(3, '白猫', '白狗', '白蛇','白兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(4, '黑猫', '黑狗', '黑蛇','黑兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(5, '黄猫', '黄狗', '黄蛇','黄兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(6, '蓝猫', '蓝狗', '蓝蛇','蓝兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(7, '绿猫', '绿狗', '绿蛇','绿兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(8, '红猫', '红狗', '红蛇','红兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(9, '青猫', '青狗', '青蛇','青兔');
INSERT INTO animal(AnimalID, Cat, Dog, Snake, Rabbit) values(10, '紫猫', '紫狗', '紫蛇','紫兔');
实际效果图
点开界面点击初始化
点下一页到最后
配置数据源
完整项目下载地址https://download.csdn.net/download/qq_37529913/12010295