📄 departments.cpp
字号:
// Departments.cpp: implementation of the CDepartments class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HrSys.h"
#include "Departments.h"
#include "ADOConn.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDepartments::CDepartments()
{
Dep_id = 0;
Dep_name = "";
Describe = "";
UpperId = 0;
}
CDepartments::~CDepartments()
{
}
//读取和设置成员变量
int CDepartments::GetDep_id()
{
return Dep_id;
}
void CDepartments::SetDep_id(int iDep_id)
{
Dep_id = iDep_id;
}
CString CDepartments::GetDep_name()
{
return Dep_name;
}
void CDepartments::SetDep_name(CString cDep_name)
{
Dep_name = cDep_name;
}
CString CDepartments::GetDescribe()
{
return Describe;
}
void CDepartments::SetDescribe(CString cDescribe)
{
Describe = cDescribe;
}
int CDepartments::GetUpperId()
{
return UpperId;
}
void CDepartments::SetUpperId(int iUpperId)
{
UpperId = iUpperId;
}
//数据库操作
int CDepartments::HaveName(CString cDep_Name)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM Departments WHERE Dep_name='" + cDep_Name + "'";
//执行SELECT语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
if (m_pRecordset->adoEOF)
return -1;
else
return 1;
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
int CDepartments::HaveSon(CString cDep_id)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM Departments WHERE UpperId=" + cDep_id;
//执行SELECT语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
if (m_pRecordset->adoEOF)
return -1;
else
return 1;
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
int CDepartments::HaveEmp(CString cDep_id)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM Employees WHERE Dep_Id=" + cDep_id;
//执行SELECT语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
if (m_pRecordset->adoEOF)
return -1;
else
return 1;
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
void CDepartments::Load_dep()
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM Departments ORDER BY UpperId";
//执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
//初始化数组
a_DepName.RemoveAll();
a_DepId.RemoveAll();
a_UpperId.RemoveAll();
//执行SELECT语句
while (m_pRecordset->adoEOF == 0)
{
a_DepId.Add((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Dep_Id"));
a_DepName.Add((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Dep_Name"));
a_UpperId.Add((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("UpperId"));
m_pRecordset->MoveNext();
}
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
long CDepartments::sql_insert()
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置INSERT语句
CString strUpperId;
strUpperId.Format("%d", UpperId);
_bstr_t vSQL;
vSQL = "INSERT INTO Departments (Dep_name, Describe, UpperId) VALUES('"
+ Dep_name + "','" + Describe + "'," + strUpperId + ")";
//执行INSERT语句
m_AdoConn.ExecuteSQL(vSQL);
//读取最大编号
long lDepId;
vSQL = "SELECT MAX(Dep_id) AS MaxId FROM Departments";
//执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
//返回各列的值
if (m_pRecordset->adoEOF)
lDepId = 0;
else
lDepId = atol((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("MaxId"));
//断开与数据库的连接
m_AdoConn.ExitConnect();
return lDepId;
}
void CDepartments::sql_update(CString cDepId)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置UPDATE语句
_bstr_t vSQL;
vSQL = "UPDATE Departments SET Dep_name='" + Dep_name
+ "', Describe='" + Describe +"' WHERE Dep_id=" + cDepId;
//执行UPDATE语句
m_AdoConn.ExecuteSQL(vSQL);
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
void CDepartments::sql_delete(CString cDepId)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置DELETE语句
_bstr_t vSQL;
vSQL = "DELETE FROM Departments WHERE Dep_id=" + cDepId;
//执行DELETE语句
m_AdoConn.ExecuteSQL(vSQL);
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
//根据员工编号读取所有字段值
void CDepartments::GetData(CString cDepId)
{
//连接数据库
ADOConn m_AdoConn;
m_AdoConn.OnInitADOConn();
//设置SELECT语句
_bstr_t vSQL;
vSQL = "SELECT * FROM Departments WHERE Dep_id=" + cDepId;
//执行SELETE语句
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
//返回各列的值
if (m_pRecordset->adoEOF)
CDepartments();
else
{
Dep_id = atoi(cDepId);
Dep_name = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Dep_Name");
Describe = (LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("Describe");
UpperId = atoi((LPCTSTR)(_bstr_t)m_pRecordset->GetCollect("UpperId"));
}
//断开与数据库的连接
m_AdoConn.ExitConnect();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -