📄 student.cpp
字号:
// Student.cpp : implementation file
//
#include "stdafx.h"
#include "jwc.h"
#include "Student.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStudent dialog
CStudent::CStudent(CWnd* pParent /*=NULL*/)
: CDialog(CStudent::IDD, pParent)
{
//{{AFX_DATA_INIT(CStudent)
m_birth = _T("");
m_class = _T("");
m_Count = 0;
m_dept = _T("");
m_name = _T("");
m_number = _T("");
m_sex = _T("");
i=0;
m_search = _T("");
//}}AFX_DATA_INIT
}
void CStudent::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStudent)
DDX_Text(pDX, IDC_Birth, m_birth);
DDX_Text(pDX, IDC_Class, m_class);
DDX_Text(pDX, IDC_Count, m_Count);
DDX_Text(pDX, IDC_Dept, m_dept);
DDX_Text(pDX, IDC_Name, m_name);
DDX_Text(pDX, IDC_Number, m_number);
DDX_Text(pDX, IDC_Sex, m_sex);
DDX_Text(pDX, IDC_Search, m_search);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStudent, CDialog)
//{{AFX_MSG_MAP(CStudent)
ON_BN_CLICKED(IDC_BUTTON7, Previous_One)
ON_BN_CLICKED(IDC_BUTTON6, First_One)
ON_BN_CLICKED(IDC_BUTTON8, Next_One)
ON_BN_CLICKED(IDC_BUTTON9, Last_One)
ON_BN_CLICKED(IDC_Add, OnAdd)
ON_BN_CLICKED(IDC_Modify, OnModify)
ON_BN_CLICKED(IDC_Delete, OnDelete)
ON_WM_CTLCOLOR()
ON_WM_PAINT()
ON_BN_CLICKED(IDC_BUTTON11, Search)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStudent message handlers
HBRUSH CStudent::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
// TODO: Return a different brush if the default is not desired
return hbr;
}
void CStudent::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// Do not call CDialog::OnPaint() for painting messages
}
BOOL CStudent::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_pRecordset.CreateInstance(__uuidof(Recordset));
// TODO: Add extra initialization here
CString str="SELECT * FROM Student";
Test(str);
Read(0);
m_Count=Count();
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CStudent::Test(CString str)
{
if ( m_pRecordset->State )
m_pRecordset->Close();
_variant_t str1=str;
try
{
m_pRecordset->Open(str1, // 查询Student表中所有字段
m_pConnection.GetInterfacePtr(), // 获取库接库的IDispatch指针
adOpenDynamic,
adLockOptimistic,
adCmdText);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
void CStudent::Read(int i)
{
// 在ADO操作中建议语句中要常用try...catch()来捕获错误信息,
// 因为它有时会经常出现一些意想不到的错误。jingzhou xu
_variant_t var;
try
{
if(!m_pRecordset->adoBOF)
m_pRecordset->Move(i);
else
{
AfxMessageBox("表内数据为空");
return ;
}
// 读入库中各字段并加入列表框中
var = m_pRecordset->GetCollect("Sno");
if(var.vt != VT_NULL)
m_number = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Sna");
if(var.vt != VT_NULL)
m_name = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Dna");
if(var.vt != VT_NULL)
m_dept = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Cno");
if(var.vt != VT_NULL)
m_class = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Ssex");
if(var.vt != VT_NULL)
m_sex = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Sbirth");
if(var.vt != VT_NULL)
m_birth = (LPCSTR)_bstr_t(var);
UpdateData(false);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
int CStudent::Count()
{
CString str="SELECT COUNT(*) FROM Student";
Test(str);
_variant_t vIndex = (long)0;
_variant_t vCount = m_pRecordset->GetCollect(vIndex);///取得第一个字段的值放入vCount变量
m_pRecordset->Close();///关闭记录集
int x=vCount.lVal;
return x;
}
void CStudent::Previous_One()
{
// TODO: Add your control notification handler code here
if (i==0)
{
MessageBox("已经到达第一个!");
}
else
{
i--;
CString str="SELECT * FROM Student";
Test(str);
Read(i);
}
}
void CStudent::First_One()
{
// TODO: Add your control notification handler code here
CString str="SELECT * FROM Student";
Test(str);
Read(0);
}
void CStudent::Next_One()
{
// TODO: Add your control notification handler code here
if (i==Count()-1)
{
MessageBox("已经到达最后一个!");
}
else
{
i++;
CString str="SELECT * FROM Student";
Test(str);
Read(i);
}
}
void CStudent::Last_One()
{
// TODO: Add your control notification handler code here
i=Count()-1;
CString str="SELECT * FROM Student";
Test(str);
Read(i);
}
void CStudent::Execute(CString str)
{
_variant_t recordsaffected;
m_pConnection->Execute((_bstr_t)str,&recordsaffected,0);
}
void CStudent::OnAdd()
{
// TODO: Add your control notification handler code here
try
{
UpdateData(true);
// CString sql;
// sql.Format("Select * from Student where Sno='' ;",m_number);
// Execute(sql);
//
// _variant_t var;
// CString x;
// var = m_pRecordset->GetCollect("Sno");
// if(var.vt != VT_NULL)
// x = (LPCSTR)_bstr_t(var);
// MessageBox(x);
//
// // if(m_pConnection==)
CString sql1;
sql1.Format("Insert into Student values ('%s', '%s' , '%s', '%s' , '%s' , '%s' )",
m_number, m_name, m_sex , m_class , m_birth, m_dept );
Execute(sql1);
MessageBox("添加成功!");
m_Count=Count();
UpdateData(false);
}
catch (CMemoryException* e)
{
MessageBox("添加失败!");
}
}
void CStudent::OnModify()
{
// TODO: Add your control notification handler code here
try
{
UpdateData(true);
CString sql;
sql.Format(" UPDATE Student SET Sna = '%s' ,Ssex ='%s', Cno = '%s' , Sbirth = '%s', Dna= '%s'where Sno = '%s' ; ",
m_name, m_sex , m_class , m_birth, m_dept,m_number );
Execute(sql);
MessageBox("修改成功!");
}
catch (CMemoryException* e)
{
MessageBox("修改失败!");
}
}
void CStudent::OnDelete()
{
// TODO: Add your control notification handler code here
try
{
if(MessageBox( "确定删除该记录?","删除记录", MB_YESNO ) == IDYES)
{
UpdateData(true);
CString sql;
sql.Format(" Delete from Student where Sno = '%s' ; ",m_number );
Execute(sql);
MessageBox("删除成功!");
m_Count=Count();
UpdateData(false);
}
}
catch (CMemoryException* e)
{
MessageBox("删除失败!");
}
}
void CStudent::Search()
{
// TODO: Add your control notification handler code here
try
{
UpdateData(true);
CString sql;
sql.Format(" Select * from Student where Sno = '%s' ; ", m_search);
Test(sql);
read();
}
catch (CMemoryException* e)
{
MessageBox("查找失败!");
}
}
void CStudent::read()
{
_variant_t var;
try
{
var = m_pRecordset->GetCollect("Sno");
if(var.vt != VT_NULL)
m_number = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Sna");
if(var.vt != VT_NULL)
m_name = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Dna");
if(var.vt != VT_NULL)
m_dept = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Cno");
if(var.vt != VT_NULL)
m_class = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Ssex");
if(var.vt != VT_NULL)
m_sex = (LPCSTR)_bstr_t(var);
var = m_pRecordset->GetCollect("Sbirth");
if(var.vt != VT_NULL)
m_birth = (LPCSTR)_bstr_t(var);
UpdateData(false);
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -