2022-07-04 00:31

This commit is contained in:
zyx 2022-07-04 00:31:52 +08:00
parent a8f69e824b
commit a0109d2c99
13 changed files with 669 additions and 27 deletions

View File

@ -10,7 +10,8 @@
void addCourse(_ConnectionPtr connection, Course course);
void deleteCourse(_ConnectionPtr connection, string id);
void updateCourseName(_ConnectionPtr connection, string id, string name);
Course& getCourseById(_ConnectionPtr connection, string id);
std::vector<Course>& getCourseByName(_ConnectionPtr connection, string name);
Course* getCourseById(_ConnectionPtr connection, string id);
Course* getCourseByName(_ConnectionPtr connection, string name);
std::vector<Course>* getAllCourse(_ConnectionPtr connection);
#endif // !COURSE_DAO_H

View File

@ -19,19 +19,32 @@ void updateCourseName(_ConnectionPtr connection, string id, string name) {
connection->Execute(sql.c_str(), NULL, (long)0);
}
Course& getCourseById(_ConnectionPtr connection, string id) {
Course* getCourseById(_ConnectionPtr connection, string id) {
string sql = "select * from course where id = '" + id + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
Course* course = new Course();
course->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value);
course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value);
return *course;
return course;
}
std::vector<Course>& getCourseByName(_ConnectionPtr connection, string name) {
Course* getCourseByName(_ConnectionPtr connection, string name) {
string sql = "select * from course where name = '" + name + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
Course* course = new Course();
course->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value);
course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value);
return course;
}
std::vector<Course>* getAllCourse(_ConnectionPtr connection) {
string sql = "select * from course";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
std::vector<Course>* v = new std::vector<Course>();
Course* course;
@ -41,5 +54,5 @@ std::vector<Course>& getCourseByName(_ConnectionPtr connection, string name) {
course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value);
v->push_back(*course);
}
return *v;
return v;
}

View File

@ -12,8 +12,16 @@ void deleteOnesGrade(_ConnectionPtr connection, string stuId,string courseId);
void deleteGradeByStuId(_ConnectionPtr connection, string stuId);
void deleteGradeByCourseId(_ConnectionPtr connection, string courseId);
void updateOnesGrade(_ConnectionPtr connection, string stuId, string courseId, string grade);
Grade& getOnesGrade(_ConnectionPtr connection, string stuId, string courseId);
std::vector<Grade>& getGradeByStuId(_ConnectionPtr connection, string stuId);
std::vector<Grade>& getGradeByCourseId(_ConnectionPtr connection, string courseId);
Grade* getOnesGrade(_ConnectionPtr connection, string stuId, string courseId);
std::vector<Grade>* getGradeByStuId(_ConnectionPtr connection, string stuId);
std::vector<Grade>* getGradeByCourseId(_ConnectionPtr connection, string courseId);
std::vector<Grade>* getGradeByStuClass(_ConnectionPtr connection, string stuClass);
std::vector<Grade>* getAllGrade(_ConnectionPtr connection);
double getOnesAvgGrade(_ConnectionPtr connection, string stuId);
double getOnesAllGrade(_ConnectionPtr connection, string stuId);
double getClassAvgGrade(_ConnectionPtr connection, string stuClass, string courseName);
double getClassMaxGrade(_ConnectionPtr connection, string stuClass, string courseName);
double getClassMinGrade(_ConnectionPtr connection, string stuClass, string courseName);
#endif // !GRADE_DAO_H

View File

@ -32,20 +32,22 @@ void updateOnesGrade(_ConnectionPtr connection, string stuId, string courseId, s
connection->Execute(sql.c_str(), NULL, (long)0);
}
Grade& getOnesGrade(_ConnectionPtr connection, string stuId,string courseId) {
Grade* getOnesGrade(_ConnectionPtr connection, string stuId,string courseId) {
string sql = "select * from grade where stuId = '" + stuId + "\' and courseId = \'" + courseId + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
Grade* grade = new Grade();
grade->setStuId((char*)(_bstr_t)record->Fields->GetItem("stuId")->Value);
grade->setCourseId((char*)(_bstr_t)record->Fields->GetItem("courseId")->Value);
grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value);
return *grade;
return grade;
}
std::vector<Grade>& getGradeByStuId(_ConnectionPtr connection, string stuId) {
std::vector<Grade>* getGradeByStuId(_ConnectionPtr connection, string stuId) {
string sql = "select * from grade where stuId = '" + stuId + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
std::vector<Grade>* v = new std::vector<Grade>();
Grade* grade;
@ -56,12 +58,13 @@ std::vector<Grade>& getGradeByStuId(_ConnectionPtr connection, string stuId) {
grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value);
v->push_back(*grade);
}
return *v;
return v;
}
std::vector<Grade>& getGradeByCourseId(_ConnectionPtr connection, string courseId) {
std::vector<Grade>* getGradeByCourseId(_ConnectionPtr connection, string courseId) {
string sql = "select * from grade where courseId = '" + courseId + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
std::vector<Grade>* v = new std::vector<Grade>();
Grade* grade;
@ -72,5 +75,84 @@ std::vector<Grade>& getGradeByCourseId(_ConnectionPtr connection, string courseI
grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value);
v->push_back(*grade);
}
return *v;
return v;
}
std::vector<Grade>* getGradeByStuClass(_ConnectionPtr connection, string stuClass) {
string sql = "select * from grade where stuId in(select id from student where stuClass = '" + stuClass + "')";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
std::vector<Grade>* v = new std::vector<Grade>();
Grade* grade;
for (; !record->EndOfFile; record->MoveNext()) {
grade = new Grade();
grade->setStuId((char*)(_bstr_t)record->Fields->GetItem("stuId")->Value);
grade->setCourseId((char*)(_bstr_t)record->Fields->GetItem("courseId")->Value);
grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value);
v->push_back(*grade);
}
return v;
}
std::vector<Grade>* getAllGrade(_ConnectionPtr connection) {
string sql = "select * from grade";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
std::vector<Grade>* v = new std::vector<Grade>();
Grade* grade;
for (; !record->EndOfFile; record->MoveNext()) {
grade = new Grade();
grade->setStuId((char*)(_bstr_t)record->Fields->GetItem("stuId")->Value);
grade->setCourseId((char*)(_bstr_t)record->Fields->GetItem("courseId")->Value);
grade->setGrade((char*)(_bstr_t)record->Fields->GetItem("grade")->Value);
v->push_back(*grade);
}
return v;
}
double getOnesAvgGrade(_ConnectionPtr connection, string stuId) {
string sql = "select count(*) cnt,sum(grade) all_grade from grade where stuId = '" + stuId + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
int cnt = atoi((char*)(_bstr_t)record->Fields->GetItem("cnt")->Value);
double all_grade = atof((char*)(_bstr_t)record->Fields->GetItem("all_grade")->Value);
return all_grade / cnt;
}
double getOnesAllGrade(_ConnectionPtr connection, string stuId) {
string sql = "select sum(grade) all_grade from grade where stuId = '" + stuId + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
return atof((char*)(_bstr_t)record->Fields->GetItem("all_grade")->Value);
}
double getClassAvgGrade(_ConnectionPtr connection, string stuClass, string courseName) {
string sql = "select avg(grade) avg_grade from grade where stuId in (select id from student where stuClass = '" + stuClass + "') ";
sql += "and courseId in (select id from course where name = '" + courseName + "')";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
return atof((char*)(_bstr_t)record->Fields->GetItem("avg_grade")->Value);
}
double getClassMaxGrade(_ConnectionPtr connection, string stuClass, string courseName) {
string sql = "select max(grade) max_grade from grade where stuId in (select id from student where stuClass = '" + stuClass + "') ";
sql += "and courseId in (select id from course where name = '" + courseName + "')";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
return atof((char*)(_bstr_t)record->Fields->GetItem("max_grade")->Value);
}
double getClassMinGrade(_ConnectionPtr connection, string stuClass, string courseName) {
string sql = "select min(grade) min_grade from grade where stuId in (select id from student where stuClass = '" + stuClass + "') ";
sql += "and courseId in (select id from course where name = '" + courseName + "')";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
return atof((char*)(_bstr_t)record->Fields->GetItem("min_grade")->Value);
}

431
Menu.cpp Normal file
View File

@ -0,0 +1,431 @@
#include<iostream>
#include"Menu.h"
#include"MenuUtils.h"
#include"StudentDAO.h"
#include"GradeDAO.h"
#include"CourseDAO.h"
using namespace std;
void mainMenu(_ConnectionPtr connection) {
system("cls");
printf("1.显示所有学生\n");
printf("2.增加学生\n");
printf("3.删除学生\n");
printf("4.修改学生\n");
printf("5.从文件导入学生信息\n");
printf("6.导出学生信息到文件\n");
printf("7.学生成绩管理\n");
printf("8.课程管理\n");
printf("0.退出\n");
int opt = -1;
checkOptInput(opt, 0, 8);
switch (opt){
case 1:
showAllStudent(connection);
break;
case 2:
insertStudent(connection);
break;
case 3:
deleteStudent(connection);
break;
case 4:
changeStudent(connection);
break;
case 5:
importData(connection);
break;
case 6:
exportData(connection);
break;
case 7:
studentGradeMenu(connection);
break;
case 8:
courseMenu(connection);
break;
default:
exit(EXIT_SUCCESS);
}
printf("\n");
}
void studentGradeMenu(_ConnectionPtr connection) {
while (true) {
system("cls");
printf("1.输入学生成绩\n");
printf("2.获取学生平均成绩\n");
printf("3.获取学生总成绩\n");
printf("4.获取班级平均分\n");
printf("5.获取班级最高分\n");
printf("6.获取班级最低分\n");
printf("7.获取班级成绩\n");
printf("8.二级拓展菜单\n");
printf("0.返回上一级\n");
int opt = -1;
checkOptInput(opt, 0, 8);
printf("\n");
string stuId, courseId;
string stuClass, courseName;
double grade = -1;
int size;
vector<Grade> *v;
switch (opt) {
case 1://输入学生成绩
printf("请输入学号:");
cin >> stuId;
printf("请输入课程名:");
cin >> courseId;
printf("请输入分数(0-100)");
while (!(cin >> grade) || (opt < 0) || (opt > 100)) {
cout << "输入错误!请重新输入:";
cin.clear();
while (cin.get() != '\n');
}
addGrade(connection, *new Grade(stuId, courseId, doubleToString(grade)));
printf("添加成功!\n");
break;
case 2://获取学生平均成绩
printf("请输入学号:");
cin >> stuId;
printf("学生平均成绩为:%.2lf\n", getOnesAvgGrade(connection, stuId));
break;
case 3://获取学生总成绩
printf("请输入学号:");
cin >> stuId;
printf("学生总成绩为:%.2lf\n", getOnesAllGrade(connection, stuId));
break;
case 4://获取班级平均分
printf("请输入班级:");
cin >> stuClass;
printf("请输入课程名:");
cin >> courseName;
cout << stuClass << "班级" << courseName << "课程的平均分为:" << getClassAvgGrade(connection, stuClass, courseName) << "\n";
break;
case 5://获取班级最高分
printf("请输入班级:");
cin >> stuClass;
printf("请输入课程名:");
cin >> courseName;
cout << stuClass << "班级" << courseName << "课程的最高分为:" << getClassMaxGrade(connection, stuClass, courseName) << "\n";
break;
case 6://获取班级最低分
printf("请输入班级:");
cin >> stuClass;
printf("请输入课程名:");
cin >> courseName;
cout << stuClass << "班级" << courseName << "课程的最低分为:" << getClassMinGrade(connection, stuClass, courseName) << "\n";
break;
case 7://获取班级成绩
printf("请输入班级:");
cin >> stuClass;
v = getGradeByStuClass(connection, stuClass);
if (v == NULL) {
printf("该班级不存在!\n");
break;
}
size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n";
for (int i = 0; i < size; i++) {
cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n";
}
break;
case 8://二级拓展菜单
studentGradeExternMenu(connection);
break;
default://返回上一级
return;
}
system("pause");
}
}
void studentGradeExternMenu(_ConnectionPtr connection) {
while (true) {
system("cls");
printf("1.删除某学生某科成绩\n");
printf("2.删除某学生成绩\n");
printf("3.删除某科成绩\n");
printf("4.更改某学生某科成绩\n");
printf("5.查询某学生某科成绩\n");
printf("6.查询某学生成绩\n");
printf("7.查询某科成绩\n");
printf("8.查询所有成绩\n");
printf("0.返回上一级\n");
int opt = -1;
checkOptInput(opt, 0, 8);
printf("\n");
string stuId, courseId;
string stuClass, courseName;
double grade = -1;
int size;
vector<Grade> *v;
Grade* gra;
Course* course;
switch (opt) {
case 1://删除某学生某科成绩
printf("请输入学号:");
cin >> stuId;
printf("请输入课程名:");
cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
deleteOnesGrade(connection, stuId, course->getId());
printf("删除成功!\n");
break;
case 2://删除某学生成绩
printf("请输入学号:");
cin >> stuId;
deleteGradeByStuId(connection, stuId);
printf("删除成功!\n");
break;
case 3://删除某科成绩
printf("请输入课程名:");
cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
deleteGradeByCourseId(connection, course->getId());
printf("删除成功!\n");
break;
case 4://更改某学生某科成绩
printf("请输入学号:");
cin >> stuId;
printf("请输入课程名:");
cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
printf("请输入新分数(0-100)");
while (!(cin >> grade) || (opt < 0) || (opt > 100)) {
cout << "输入错误!请重新输入:";
cin.clear();
while (cin.get() != '\n');
}
updateOnesGrade(connection, stuId, course->getId(), doubleToString(grade));
printf("更改成功!\n");
break;
case 5://查询某学生某科成绩
printf("请输入学号:");
cin >> stuId;
printf("请输入课程名:");
cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
gra = getOnesGrade(connection, stuId, course->getId());
if (gra == NULL) {
printf("该条记录不存在!\n");
break;
}
cout << "学号\t" << "课程名\t" << "成绩\n";
cout << (*gra).getStuId() << "\t" << getCourseById(connection, (*gra).getCourseId())->getName() << "\t" << (*gra).getGradeStr() << "\n";
break;
case 6://查询某学生成绩
printf("请输入学号:");
cin >> stuId;
v = getGradeByStuId(connection, stuId);
if (v == NULL) {
printf("该学生不存在!\n");
break;
}
size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n";
for (int i = 0; i < size; i++) {
cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n";
}
break;
case 7://查询某科成绩
printf("请输入课程名:");
cin >> courseName;
course = getCourseByName(connection, courseName);
if (course == NULL) {
printf("该课程不存在!\n");
break;
}
v = getGradeByCourseId(connection, course->getId());
if (v == NULL) {
printf("该课程不存在对应成绩!\n");
break;
}
size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n";
for (int i = 0; i < size; i++) {
cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n";
}
break;
case 8://查询所有成绩
v = getAllGrade(connection);
if (v == NULL) {
printf("无成绩记录!\n");
break;
}
size = v->size();
cout << "学号\t" << "课程名\t" << "成绩\n";
for (int i = 0; i < size; i++) {
cout << (*v)[i].getStuId() << "\t" << getCourseById(connection, (*v)[i].getCourseId())->getName() << "\t" << (*v)[i].getGradeStr() << "\n";
}
break;
default://返回上一级
return;
}
system("pause");
}
}
void courseMenu(_ConnectionPtr connection) {
while (true) {
system("cls");
printf("1.添加课程\n");
printf("2.删除课程\n");
printf("3.更改课程\n");
printf("4.查询所有课程\n");
printf("0.返回上一级\n");
int opt = -1;
checkOptInput(opt, 0, 8);
printf("\n");
int size;
string courseId, courseName;
vector<Course>* v;
switch (opt) {
case 1://添加课程
printf("请输入课程号:");
cin >> courseId;
printf("请输入课程名:");
cin >> courseName;
addCourse(connection,*new Course(courseId, courseName));
printf("添加成功!\n");
break;
case 2://删除课程
printf("请输入课程号:");
cin >> courseId;
deleteCourse(connection, courseId);
printf("删除成功!\n");
break;
case 3://更改课程
printf("请输入课程号:");
cin >> courseId;
printf("请输入课程名:");
cin >> courseName;
updateCourseName(connection, courseId,courseName);
printf("更新成功!\n");
break;
case 4://查询所有课程
v = getAllCourse(connection);
if (v == NULL) {
printf("不存在课程记录!\n");
break;
}
size = v->size();
cout << "课程号\t" << "课程名\n";
for (int i = 0; i < size; i++) {
cout << (*v)[i].getId() << "\t" << (*v)[i].getName() << "\n";
}
break;
default://返回上一级
return;
}
system("pause");
}
}
void showAllStudent(_ConnectionPtr connection) {
vector<Student> vs = *getAllStudent(connection);
int size = vs.size();
for (int i = 0; i < size; i++) {
cout << vs[i].toString() << "\n";
}
}
void insertStudent(_ConnectionPtr connection) {
Student *stu = new Student();
string str;
printf("请输入学号:");
cin >> str;
stu->setId(str);
printf("请输入姓名:");
cin >> str;
stu->setName(str);
printf("请输入性别:");
cin >> str;
stu->setSex(str);
printf("请输入班级:");
cin >> str;
stu->setStuClass(str);
printf("请输入学生状态(在读、休学、退学)");
cin >> str;
stu->setStatus(str);
addStudent(connection, *stu);
printf("添加成功!\n");
}
void deleteStudent(_ConnectionPtr connection) {
string id;
printf("请输入待删除学生的学号:");
cin >> id;
Student *stu = getStudentById(connection,id);
if (stu == NULL) {
printf("该学生不存在!\n");
return;
}
deleteStudent(connection, id);
printf("删除成功!\n");
}
void changeStudent(_ConnectionPtr connection) {
string id;
printf("请输入待修改学生的学号:");
cin >> id;
Student* stu = getStudentById(connection, id);
if (stu == NULL) {
printf("该学生不存在!\n");
return;
}
printf("请选择修改 班级(0) / 学生状态(1)");
int opt = -1;
checkOptInput(opt, 0, 1);
string input;
switch (opt) {
case 0:
printf("\n请输入新班级:");
cin >> input;
updateStudentClass(connection,id,input);
break;
default:
printf("\n请输入新状态:");
cin >> input;
updateStudentStatus(connection,id, input);
break;
}
}
void exportData(_ConnectionPtr connection) {
}
void importData(_ConnectionPtr connection) {
}

24
Menu.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef MENU_H
#define MENU_H
#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include<vector>
#include"Student.cpp"
#include"Course.cpp"
#include"Grade.cpp"
void mainMenu(_ConnectionPtr connection);
void studentGradeMenu(_ConnectionPtr connection);
void studentGradeExternMenu(_ConnectionPtr connection);
void courseMenu(_ConnectionPtr connection);
void showAllStudent(_ConnectionPtr connection);
void insertStudent(_ConnectionPtr connection);
void deleteStudent(_ConnectionPtr connection);
void changeStudent(_ConnectionPtr connection);
void exportData(_ConnectionPtr connection);
void importData(_ConnectionPtr connection);
#endif // !MENU_H

29
MenuUtils.cpp Normal file
View File

@ -0,0 +1,29 @@
#include"MenuUtils.h"
void checkOptInput(int& opt, int low, int high) {
printf(">>>请输入选项(%d-%d)", low, high);
while (!(cin >> opt) || (opt < low) || (opt > high)) {
cout << "输入选项错误!请重新输入:";
cin.clear();
while (cin.get() != '\n');
}
}
string doubleToString(double num) {
char* buf;
int dec, sign;
buf = _fcvt(num, 2, &dec, &sign);
string str;
if (sign) str += "-";
int i;
for (i = 0; i < dec; i++) {
str += buf[i];
}
str += ".";
for (; i < strlen(buf); i++) {
str += buf[i];
}
return str;
}

12
MenuUtils.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef MENU_UTILS_H
#define MENU_UTILS_H
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
using namespace std;
void checkOptInput(int& opt, int low, int high);
string doubleToString(double num);
#endif // !MENU_UTILS_H

View File

@ -133,6 +133,8 @@
<ClCompile Include="Grade.cpp" />
<ClCompile Include="GradeDAOImpl.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Menu.cpp" />
<ClCompile Include="MenuUtils.cpp" />
<ClCompile Include="Student.cpp" />
<ClCompile Include="StudentDAOImpl.cpp" />
<ClCompile Include="Test.cpp" />
@ -141,6 +143,8 @@
<ClInclude Include="CourseDAO.h" />
<ClInclude Include="DBCUtils.h" />
<ClInclude Include="GradeDAO.h" />
<ClInclude Include="Menu.h" />
<ClInclude Include="MenuUtils.h" />
<ClInclude Include="StudentDAO.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

View File

@ -42,6 +42,12 @@
<ClCompile Include="Test.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Menu.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="MenuUtils.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="DBCUtils.h">
@ -56,5 +62,11 @@
<ClInclude Include="GradeDAO.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="Menu.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="MenuUtils.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -11,7 +11,8 @@ void addStudent(_ConnectionPtr connection, Student student);
void deleteStudent(_ConnectionPtr connection, string id);
void updateStudentClass(_ConnectionPtr connection, string id, string stuClass);
void updateStudentStatus(_ConnectionPtr connection, string id, string status);
Student& getStudentById(_ConnectionPtr connection, string id);
std::vector<Student>& getStudentByName(_ConnectionPtr connection, string name);
Student* getStudentById(_ConnectionPtr connection, string id);
std::vector<Student>* getStudentByName(_ConnectionPtr connection, string name);
std::vector<Student>* getAllStudent(_ConnectionPtr connection);
#endif // !STUDENT_DAO_H

View File

@ -28,9 +28,10 @@ void updateStudentStatus(_ConnectionPtr connection, string id, string status) {
connection->Execute(sql.c_str(), NULL, (long)0);
}
Student& getStudentById(_ConnectionPtr connection, string id) {
Student* getStudentById(_ConnectionPtr connection, string id) {
string sql = "select * from student where id = '" + id + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
Student *student = new Student();
student->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value);
@ -38,12 +39,13 @@ Student& getStudentById(_ConnectionPtr connection, string id) {
student->setSex((char*)(_bstr_t)record->Fields->GetItem("sex")->Value);
student->setStuClass((char*)(_bstr_t)record->Fields->GetItem("stuClass")->Value);
student->setStatus((char*)(_bstr_t)record->Fields->GetItem("status")->Value);
return *student;
return student;
}
std::vector<Student>& getStudentByName(_ConnectionPtr connection, string name) {
std::vector<Student>* getStudentByName(_ConnectionPtr connection, string name) {
string sql = "select * from student where name = '" + name + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
std::vector<Student> *v = new std::vector<Student>();
Student* student;
@ -56,5 +58,24 @@ std::vector<Student>& getStudentByName(_ConnectionPtr connection, string name) {
student->setStatus((char*)(_bstr_t)record->Fields->GetItem("status")->Value);
v->push_back(*student);
}
return *v;
return v;
}
std::vector<Student>* getAllStudent(_ConnectionPtr connection) {
string sql = "select * from student";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;
std::vector<Student>* v = new std::vector<Student>();
Student* student;
for (; !record->EndOfFile; record->MoveNext()) {
student = new Student();
student->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value);
student->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value);
student->setSex((char*)(_bstr_t)record->Fields->GetItem("sex")->Value);
student->setStuClass((char*)(_bstr_t)record->Fields->GetItem("stuClass")->Value);
student->setStatus((char*)(_bstr_t)record->Fields->GetItem("status")->Value);
v->push_back(*student);
}
return v;
}

View File

@ -4,9 +4,10 @@
#include <iostream>
#include <locale.h> // setlocale
#include <string>
#include<vector>
#include <vector>
#include "DBCUtils.h"
#include "StudentDAO.h"
#include "Menu.h"
using namespace std;
//高级功能 直接输入sql语句
@ -22,11 +23,14 @@ int main() {
_ConnectionPtr pconnect(_uuidof(Connection));
if (getConnection(pwd, pconnect)) {
try {
}catch (_com_error& err) {
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
while (true) {
try {
mainMenu(pconnect);
}
catch (_com_error& err) {
wprintf(L"The application throws the error: %s\n", (wchar_t*)err.ErrorMessage());
wprintf(L"Description = %s\n", (wchar_t*)err.Description());
}
}
}
CoUninitialize();