⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stdlg.cpp

📁 具有添加,删除,修改,更新的功能,用ado读ini文件访问数据库
💻 CPP
字号:
// StDlg.cpp : implementation file
//

#include "stdafx.h"
#include "学生成绩管理系统.h"
#include "StDlg.h"
#include "GlobalVarFunc.h"
#include "DBOper.h"
#include "ado.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CStDlg dialog
//数据库相关变量
extern char gServerName[128];
extern char gDataBaseName[200];
extern char gUserName[128];
extern char gPassWD[128];

extern	CADODatabase *gpDataBase;
extern	bool         gDBConnected;
extern	COleDateTime gDBConnTime;
extern  CADORecordset *pRs; 


CStDlg::CStDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CStDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CStDlg)
	m_stno = _T("");
	m_stname = _T("");
	m_sex = _T("");
	m_age = 0;
	//}}AFX_DATA_INIT
}


void CStDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CStDlg)
	DDX_Control(pDX, IDC_STLIST, m_stlist);
	DDX_Text(pDX, IDC_EDIT1, m_stno);
	DDX_Text(pDX, IDC_EDIT3, m_stname);
	DDX_Text(pDX, IDC_EDIT4, m_sex);
	DDX_Text(pDX, IDC_EDIT5, m_age);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CStDlg, CDialog)
	//{{AFX_MSG_MAP(CStDlg)
	ON_BN_CLICKED(IDC_ADDSTBUTTON, OnAddstbutton)
	ON_BN_CLICKED(IDC_DELSTBUTTON, OnDelstbutton)
	ON_BN_CLICKED(IDC_MODSTBUTTON, OnModstbutton)
	ON_NOTIFY(NM_CLICK, IDC_STLIST, OnClickStlist)
	ON_BN_CLICKED(IDC_FINDSTBUTTON, OnFindstbutton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CStDlg message handlers

void CStDlg::OnAddstbutton() 
{
	// TODO: Add your control notification handler code here
	StInfo st;
	bool        ret;
	UpdateData(true);
	
	strcpy(st.st_no,m_stno);
	strcpy(st.st_name,m_stname);
	strcpy(st.st_sex,m_sex);
	st.st_age=m_age;
	if(strlen(st.st_no)<=0)
	{
		MessageBox("主码不能为空!");
		return;
	}

	if( false == gDBConnected )
	{
		MessageBox("数据库尚未连接!");
		return;
	}

	ret=InsertInfoIntoStudentTable(st);
	if( true == ret)
	{
		MessageBox("记录插入成功!");
		m_stno="";
		m_age=0;
		m_sex="";
		m_stname="";
	}
	else
	{
		MessageBox("记录插入失败!");
		return;
	}
	UpdateData(false);
	ReadDb();
}

void CStDlg::OnDelstbutton() 
{
	// TODO: Add your control notification handler code here
	StInfo st;
	bool        ret;

	UpdateData(true);
	
	strcpy(st.st_no,m_stno);
	if( false == gDBConnected )
	{
		MessageBox("数据库尚未连接!");
		return;
	}
	if(strlen(st.st_no)<=0)
	{
		MessageBox("学号不能为空!");
		return;
	}


	ret = DeleteInfoFromStudentTable(st);
	if( true == ret)
	{
		MessageBox("记录删除成功!");
		m_stno="";
		m_age=0;
		m_sex="";
		m_stname="";
	}
	else
	{
		MessageBox("用户不存在或访问数据表出错!");
		return;
	}
    UpdateData(false);
	ReadDb();
}

void CStDlg::OnModstbutton() 
{
	// TODO: Add your control notification handler code here
	StInfo st;
	bool        ret;
	UpdateData(true);
	
	strcpy(st.st_no,m_stno);
	strcpy(st.st_name,m_stname);
	strcpy(st.st_sex,m_sex);
	st.st_age=m_age;
	if(strlen(st.st_no)<=0)
	{
		MessageBox("主码不能为空!");
		return;
	}

	if( false == gDBConnected )
	{
		MessageBox("数据库尚未连接!");
		return;
	}
	ret=UpdateInfoFromStudentTable(st);
	if( true == ret)
	{
		MessageBox("修改成功!");
		m_stno="";
		m_age=0;
		m_sex="";
		m_stname="";
	}
	else
	{
		MessageBox("修改失败!");
		return;
	}
    UpdateData(false);
	ReadDb();
}



void CStDlg::ReadDb()
{
	CString stno,stname,stsex,stage,temp;
	char  strSql[400];
	strcpy(strSql,"select * from 学生表");
	m_stlist.DeleteAllItems();
	CADORecordset *pRs=NULL;
	pRs = new CADORecordset(gpDataBase);
	if(!pRs) return;
	if(pRs->Open(strSql)==false)
	{
		//AfxMessageBox("QQ!");
		delete pRs;
		return ;
	}
	try
	{
		if(pRs->IsBOF())
		{
			AfxMessageBox("表内数据为空");
			return ;
		}
        else
		{
			pRs->MoveFirst();
		}
		// 读入库中各字段并加入列表框中
		while(!pRs->IsEOF())
		{
			pRs->GetFieldValue("学号",temp);
			temp.TrimRight();
			stno=temp;
			pRs->GetFieldValue("姓名",temp);
			temp.TrimRight();
			stname=temp;
			pRs->GetFieldValue("性别",temp);
			temp.TrimRight();
			stsex=temp;
			pRs->GetFieldValue("年龄",temp);
			temp.TrimRight();
			stage=temp;
			LV_ITEM l; 
            int index = m_stlist.GetItemCount(); 
            l.mask = LVIF_TEXT; 
            l.iSubItem = 0; 
            l.iItem = index; 
            l.pszText = (char*)(LPCTSTR)stno; 

            m_stlist.InsertItem(&l); 
            m_stlist.SetItemText( index, 1, stname); 
            m_stlist.SetItemText( index, 2, stsex); 
            m_stlist.SetItemText( index, 3, stage); 

			UpdateData(false);
			pRs->MoveNext();
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

}

BOOL CStDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE ; 
    m_stlist.SetExtendedStyle(dwExStyle); 

	m_stlist.InsertColumn(0,"学号",LVCFMT_CENTER,80);
	m_stlist.InsertColumn(1,"姓名",LVCFMT_CENTER,80);
	m_stlist.InsertColumn(2,"性别",LVCFMT_CENTER,80);
	m_stlist.InsertColumn(3,"年龄",LVCFMT_CENTER,80);
   
	ReadDb();

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CStDlg::OnClickStlist(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int curSel = m_stlist.GetSelectionMark();	
	int i;
	char strSql[40];
	CString temp; 
	long age;

	if(false==gDBConnected )
	{
		MessageBox("数据库尚未连接!");
		return;
	}
	if(curSel<0)
		return;

	try
	{
		// 先将指针移向第一条记录,然后就可以相对第一条记录来随意移动记录指针
		pRs=new CADORecordset(gpDataBase);
	    if(!pRs) return;
		strcpy(strSql,"select * from 学生表");
	    if(pRs->Open(strSql)==false)
		{
			delete pRs;
		    return;
		}
		else
		{
			pRs->MoveFirst();
		}
		for(i=1;i<=curSel;i++)
		{
			//pRs->Move(long(curSel));
			pRs->MoveNext();
		}

		pRs->GetFieldValue("学号",temp);
		temp.TrimRight();
		m_stno=temp;
		
		pRs->GetFieldValue("姓名",temp);
		temp.TrimRight();
		m_stname=temp;
		
		pRs->GetFieldValue("性别",temp);
		temp.TrimRight();
		m_sex=temp;

		pRs->GetFieldValue("年龄",age);
        m_age=age;

	    UpdateData(false);	 
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}

	*pResult = 0;
}

void CStDlg::OnFindstbutton() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CString temp;
	char strSql[400];
	long age;
	strcpy(strSql,"select * from 学生表 where 学号='");
	strcat(strSql,m_stno);
	strcat(strSql,"'");
	CADORecordset *pRs=NULL;
	pRs = new CADORecordset(gpDataBase);
	if(!pRs) return;
	if(pRs->Open(strSql)==false)
	{
		//AfxMessageBox("QQ!");
		delete pRs;
		return ;
	}
	try
	{
		if(pRs->IsBOF())
		{
			AfxMessageBox("表内无此数据");
			return;
		}

		// 读入库中各字段并加入列表框中
		else
		{
			pRs->GetFieldValue("姓名",temp);
			temp.TrimRight();
			m_stname=temp;
			pRs->GetFieldValue("性别",temp);
			temp.TrimRight();
			m_sex=temp;
			pRs->GetFieldValue("年龄",age);
			m_age=age;
		}
	}
	catch(_com_error *e)
	{
		AfxMessageBox(e->ErrorMessage());
	}
    UpdateData(false);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -