SAS/StudentDAOImpl.cpp

81 lines
3.2 KiB
C++
Raw Normal View History

2022-07-03 18:22:02 +08:00
#include"StudentDAO.h"
void addStudent(_ConnectionPtr connection, Student student) {
string sql = "insert into student values('" + student.getId() + "'," +
"'" + student.getName() + "'," +
"'" + student.getSex() + "'," +
"'" + student.getStuClass() + "'," +
"'" + student.getStatus() + "')";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void deleteStudent(_ConnectionPtr connection, string id) {
string sql = "delete from student where id = '" + id + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void updateStudentClass(_ConnectionPtr connection, string id, string stuClass) {
string sql = "update student set stuClass = '" + stuClass + "\' where id = \'" + id + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void updateStudentStatus(_ConnectionPtr connection, string id, string status) {
string sql = "update student set status = '" + status + "\' where id = \'" + id + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
2022-07-04 00:31:52 +08:00
Student* getStudentById(_ConnectionPtr connection, string id) {
2022-07-03 18:22:02 +08:00
string sql = "select * from student where id = '" + id + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;//<2F><>¼<EFBFBD><C2BC>Ϊ<EFBFBD><CEAA>
2022-07-03 18:22:02 +08:00
Student *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);
2022-07-04 00:31:52 +08:00
return student;
2022-07-03 18:22:02 +08:00
}
2022-07-04 00:31:52 +08:00
std::vector<Student>* getStudentByName(_ConnectionPtr connection, string name) {
2022-07-03 18:22:02 +08:00
string sql = "select * from student where name = '" + name + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
if (record->BOF == -1) return NULL;//<2F><>¼<EFBFBD><C2BC>Ϊ<EFBFBD><CEAA>
2022-07-03 18:22:02 +08:00
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);
}
2022-07-04 00:31:52 +08:00
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;//<2F><>¼<EFBFBD><C2BC>Ϊ<EFBFBD><CEAA>
2022-07-04 00:31:52 +08:00
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;
2022-07-03 18:22:02 +08:00
}