Visual Studio C++数据库编程实现过程详解~21~许文俊 龚萍编制
2.3.2 数据删除
删除操作:
void CLianxi2View::OnDel() {
// TODO: Add your control notification handler code here //CAddNew dlg; CDatabase db; try {
CString str1; CString str;
if (m_pSet->IsOpen()) {
m_pSet->Close();
}
str.Format(\删除记录[%s]成功!\
str1.Format(\ db.Open(\学生信息\
db.ExecuteSQL(str1);
MessageBox(str,NULL,MB_OK|MB_ICONINFORMATION);
// MessageBox(\ }
catch (CDBException* e) {
AfxMessageBox(e->m_strError); return; } }
数据修改
注:由程序中的sql语句可以看出,使用的时候,修改是不能改学号的
2.3.3
修改操作: //////////////////
void CLianxi2View::OnMod() {
// TODO: Add your control notification handler code here CAddNew dlg; CString str; CString str1; CDatabase db;
if (IDOK==dlg.DoModal())
Visual Studio C++数据库编程实现过程详解~22~许文俊 龚萍编制
{
try{
if (m_pSet->IsOpen()) {
m_pSet->Close(); }
str1.Format(\’%s’,Sdept='%s'
WHERE Sno='%s'\
db.Open(\学生信息\
db.ExecuteSQL(str1); }
catch(CDBException *e) {
AfxMessageBox(e->m_strError); return;
}
str.Format(\修改[%s]成功!\
Visual Studio C++数据库编程实现过程详解~23~许文俊 龚萍编制
MessageBox(str,NULL,MB_OK|MB_ICONINFORMATION); }
}
2.3.4 数据查询
查询操作:
void CLianxi2View::OnCheck() { // TODO: Add your control notification handler code here // UpdateData(true);
//注:这里我updatedata函数出错,所以改用了下面三行代码,你可以试试 CString ss; GetDlgItem(IDC_CHECKNAME)-> GetWindowText(ss); m_checkname=ss; CDatabase db; db.Open(\学生信息\ CString str; m_checkname.TrimLeft(); if(m_checkname.IsEmpty()) { MessageBox(\要查询的学号不能为空! \ return; } CRecordset recset(&db); CString strSQL; strSQL.Format( \ recset.Open(CRecordset::forwardOnly, strSQL, CRecordset::readOnly); if(recset.IsEOF()) { MessageBox(\没有查到你要找的学生记录!\ } else { CString temp1, temp2, temp4; CString temp3; recset.GetFieldValue(\ recset.GetFieldValue(\ recset.GetFieldValue(\ recset.GetFieldValue(\ m_pSet->m_SNO=temp1; m_pSet->m_SNAME=temp2; m_pSet->m_SAGE=temp3; m_pSet->m_SDEPT=temp4; GetDlgItem(IDC_ID_EDIT)-> SetWindowText(temp1); GetDlgItem(IDC_NAME_EDIT)-> SetWindowText(temp2);
Visual Studio C++数据库编程实现过程详解~24~许文俊 龚萍编制
}
}
GetDlgItem(IDC_AGE_EDIT)-> SetWindowText(temp3); GetDlgItem(IDC_DEPT_EDIT)-> SetWindowText(temp4);
//注:这里的updatedata一样被我干掉了,用了上面四行代码代替。 recset.Close();
2.3.5 数据库记录显示
注:这部分跳过吧,其他都弄好了再弄。如果你成功显示状态条了,告诉我咋弄的。 这段代码如果加到程序中,不仅不能完成它应该做的,还会导致其他部分出错!! 执行函数:
BOOL CLianxi2View::OnCommand(WPARAM wParam, LPARAM lParam) {
// TODO: Add your specialized code here and/or call the base class CString str;
CMainFrame *pFrame=(CMainFrame*)AfxGetApp()->m_pMainWnd; CStatusBar*pStatus=&pFrame->m_wndStatusBar; if (pStatus) {
CRecordsetStatus rStatus; m_pSet->GetStatus(rStatus); str.Format(\当 前 记 录 : %d/
%d\
pStatus->SetPaneText(1,str); }
return CRecordView::OnCommand(wParam, lParam); }
执行结果显示
总
记
录
:
Visual Studio C++数据库编程实现过程详解~25~许文俊 龚萍编制
图 28 显示数据库记录
注:这里当然要先建好如上图28左下角所示的四个button控件
ID名分别为CreateTalbebutton DelNewtalbeButton AddNewtableButton ModButton。
2.3.6
新建表
2.9 表的操作函数
void CLianxi2View::OnCreateTalbebutton() {
// TODO: Add your control notification handler code here CDatabase db;
//if (!SQLConfigDataSource(NULL,ODBC_ADD_DSN,\学生信息\ try {
db.Open(\学生信息\ db.ExecuteSQL(\TABLE TEACHERS CHAR(4)\ }
catch (CDBException* e) {
(TeacherID