添加项目文件。

This commit is contained in:
zyx 2022-07-03 18:22:02 +08:00
parent c114019a8e
commit a8f69e824b
16 changed files with 811 additions and 0 deletions

32
Course.cpp Normal file
View File

@ -0,0 +1,32 @@
#ifndef COURSE_CPP
#define COURSE_CPP
#include<string>
using std::string;
class Course
{
public:
Course() { ; }
Course(string id, string name) {
this->id = id;
this->name = name;
}
string getId() { return id; }
void setId(string id) { this->id = id; }
string getName() { return name; }
void setName(string name) { this->name = name; }
string toString() {
string toStr = "Course{";
toStr += "id='" + id + '\'' +
", name='" + name + '\'' +
'}';
return toStr;
}
private:
string id;//¿Î³ÌºÅ
string name;//¿Î³ÌÃû
};
#endif // !COURSE_CPP

16
CourseDAO.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef COURSE_DAO_H
#define COURSE_DAO_H
#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include<iostream>
#include<vector>
#include"Course.cpp"
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);
#endif // !COURSE_DAO_H

45
CourseDAOImpl.cpp Normal file
View File

@ -0,0 +1,45 @@
#include"CourseDAO.h"
void addCourse(_ConnectionPtr connection, Course course) {
string sql = "insert into course values('" + course.getId() + "'," +
"'" + course.getName() + "')";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void deleteCourse(_ConnectionPtr connection, string id) {
string sql = "delete from course where id = '" + id + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void updateCourseName(_ConnectionPtr connection, string id, string name) {
string sql = "update course set name = '" + name + "\' where id = \'" + id + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
Course& getCourseById(_ConnectionPtr connection, string id) {
string sql = "select * from course where id = '" + id + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
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>& getCourseByName(_ConnectionPtr connection, string name) {
string sql = "select * from course where name = '" + name + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
std::vector<Course>* v = new std::vector<Course>();
Course* course;
for (; !record->EndOfFile; record->MoveNext()) {
course = new Course();
course->setId((char*)(_bstr_t)record->Fields->GetItem("id")->Value);
course->setName((char*)(_bstr_t)record->Fields->GetItem("name")->Value);
v->push_back(*course);
}
return *v;
}

73
DBCUtils.cpp Normal file
View File

@ -0,0 +1,73 @@
#include"DBCUtils.h"
bool getConnection(std::string& pwd, _ConnectionPtr& connection) {
bool isPwdTrue = false;
_bstr_t strConnect;
if (!pwd.empty()) {
try {
std::string con = "Provider=SQLOLEDB.1;Password=" + pwd + "; Persist Security Info = True; User ID = sa; Initial Catalog = stuAdminSystem; Data Source = LAPTOP-4DMOD6O5";
strConnect = con.c_str();
connection->Open(strConnect, "", "", NULL);
isPwdTrue = true;
wprintf(L"登录成功!\n");
return true;
}
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());
}
if (!isPwdTrue) {
wprintf(L"密码错误!");
return false;
}
}
else {
try {
strConnect = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=stuAdminSystem;Data Source=LAPTOP-4DMOD6O5";
connection->Open(strConnect, "", "", NULL);
isPwdTrue = true;
wprintf(L"登录成功!\n");
return true;
}
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());
}
if (!isPwdTrue) {
wprintf(L"验证失败!");
return false;
}
}
}
void getSqlType(std::string& sql, std::string& comType) {
int len = sql.length();
for (int i = 0; i < len; i++) {//判断sql语句类型
if (sql[i] != ' ')
comType += isupper(sql[i]) ? (sql[i] - 32) : sql[i];
else return;
}
}
void showRecordInfo(_RecordsetPtr& recordSet) {
COORD pos = { 0,0 };
for (long i = 0; i < recordSet->Fields->Count; i++) {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
wprintf(L"%s", (wchar_t*)recordSet->Fields->GetItem(i)->Name);
pos.X += (short)recordSet->Fields->GetItem(i)->DefinedSize % 16 + 4;
}
for (; !recordSet->EndOfFile; recordSet->MoveNext()) {
pos.X = 0;
pos.Y++;
for (long i = 0; i < recordSet->Fields->Count; i++) {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
if (recordSet->Fields->GetItem(i)->Value.vt == VT_NULL) {
wprintf(L"Null");
}
else {
wprintf(L"%s", (wchar_t*)(_bstr_t)recordSet->Fields->GetItem(i)->Value);
}
pos.X += (short)recordSet->Fields->GetItem(i)->DefinedSize % 16 + 4;
}
}
}

15
DBCUtils.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef DBCUTILS_H
#define DBCUTILS_H
#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include <stdio.h> // wprintf
#include <iostream>
#include <locale.h> // setlocale
#include <string>
bool getConnection(std::string& pwd, _ConnectionPtr& connection);
void getSqlType(std::string& sql, std::string& comType);
void showRecordInfo(_RecordsetPtr& recordSet);
#endif // !DBCUTILS_H

38
Grade.cpp Normal file
View File

@ -0,0 +1,38 @@
#ifndef GRADE_CPP
#define GRADE_CPP
#include<string>
using std::string;
class Grade {
public:
Grade() { ; }
Grade(string stuId, string courseId, string grade) {
this->stuId = stuId;
this->courseId = courseId;
this->grade = grade;
}
string getStuId() { return stuId; }
void setStuId(string stuId) { this->stuId = stuId; }
string getCourseId() { return courseId; }
void setCourseId(string courseId) { this->courseId = courseId; }
double getGrade() { return atof(grade.c_str()); }
string getGradeStr() { return grade; }
void setGrade(string grade) { this->grade = grade; }
string toString() {
string toStr = "Grade{";
toStr += "stuId='" + stuId + '\'' +
", courseId='" + courseId + '\'' +
", grade='" + grade + '\'' +
'}';
return toStr;
}
private:
string stuId;
string courseId;
string grade;
};
#endif // !GRADE_CPP

19
GradeDAO.h Normal file
View File

@ -0,0 +1,19 @@
#ifndef GRADE_DAO_H
#define GRADE_DAO_H
#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include<iostream>
#include<vector>
#include"Grade.cpp"
void addGrade(_ConnectionPtr connection, Grade grade);
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);
#endif // !GRADE_DAO_H

76
GradeDAOImpl.cpp Normal file
View File

@ -0,0 +1,76 @@
#include"GradeDAO.h"
void addGrade(_ConnectionPtr connection, Grade grade) {
string sql = "insert into grade values('" + grade.getStuId() + "'," +
"'" + grade.getCourseId() + "'," +
"'" + grade.getGradeStr() + "')";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void deleteOnesGrade(_ConnectionPtr connection, string stuId, string courseId) {
string sql = "delete from grade where stuId = '" + stuId + "\' and courseId = \'" + courseId + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void deleteGradeByStuId(_ConnectionPtr connection, string stuId) {
string sql = "delete from grade where stuId = '" + stuId + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void deleteGradeByCourseId(_ConnectionPtr connection, string courseId) {
string sql = "delete from grade where courseId = '" + courseId + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
void updateOnesGrade(_ConnectionPtr connection, string stuId, string courseId, string grade) {
string sql = "update grade set grade = " + grade + " where stuId = '" + stuId + "\' and courseId = \'" + courseId + "'";
connection->Execute(sql.c_str(), NULL, (long)0);
}
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);
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;
}
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);
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>& getGradeByCourseId(_ConnectionPtr connection, string courseId) {
string sql = "select * from grade where courseId = '" + courseId + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
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;
}

31
SAS.sln Normal file
View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "SAS", "SAS.vcxproj", "{D458779C-7429-43D9-9228-69B41BBC903B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{D458779C-7429-43D9-9228-69B41BBC903B}.Debug|x64.ActiveCfg = Debug|x64
{D458779C-7429-43D9-9228-69B41BBC903B}.Debug|x64.Build.0 = Debug|x64
{D458779C-7429-43D9-9228-69B41BBC903B}.Debug|x86.ActiveCfg = Debug|Win32
{D458779C-7429-43D9-9228-69B41BBC903B}.Debug|x86.Build.0 = Debug|Win32
{D458779C-7429-43D9-9228-69B41BBC903B}.Release|x64.ActiveCfg = Release|x64
{D458779C-7429-43D9-9228-69B41BBC903B}.Release|x64.Build.0 = Release|x64
{D458779C-7429-43D9-9228-69B41BBC903B}.Release|x86.ActiveCfg = Release|Win32
{D458779C-7429-43D9-9228-69B41BBC903B}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2D725A19-22A9-456E-B8B9-C6AB04B4FED1}
EndGlobalSection
EndGlobal

149
SAS.vcxproj Normal file
View File

@ -0,0 +1,149 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{d458779c-7429-43d9-9228-69b41bbc903b}</ProjectGuid>
<RootNamespace>SAS</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Course.cpp" />
<ClCompile Include="CourseDAOImpl.cpp" />
<ClCompile Include="DBCUtils.cpp" />
<ClCompile Include="Grade.cpp" />
<ClCompile Include="GradeDAOImpl.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Student.cpp" />
<ClCompile Include="StudentDAOImpl.cpp" />
<ClCompile Include="Test.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CourseDAO.h" />
<ClInclude Include="DBCUtils.h" />
<ClInclude Include="GradeDAO.h" />
<ClInclude Include="StudentDAO.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

60
SAS.vcxproj.filters Normal file
View File

@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Student.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Course.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Grade.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="DBCUtils.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="StudentDAOImpl.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="CourseDAOImpl.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="GradeDAOImpl.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="Test.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="DBCUtils.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="StudentDAO.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="CourseDAO.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="GradeDAO.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>

46
Student.cpp Normal file
View File

@ -0,0 +1,46 @@
#ifndef STUDENT_CPP
#define STUDENT_CPP
#include<string>
using std::string;
class Student {
public:
Student() { ; }
Student(string id, string name, string sex, string stuClass, string status) {
this->id = id;
this->name = name;
this->sex = sex;
this->stuClass = stuClass;
this->status = status;
}
string getId() { return id; }
void setId(string id) { this->id = id; }
string getName() { return name; }
void setName(string name) { this->name = name; }
string getSex() { return sex; }
void setSex(string sex) { this->sex = sex; }
string getStuClass() { return stuClass; }
void setStuClass(string stuClass) { this->stuClass = stuClass; }
string getStatus() { return status; }
void setStatus(string status) { this->status = status; }
string toString() {
string toStr = "Student{";
toStr += "id='" + id + '\'' +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", stuClass='" + stuClass + '\'' +
", status='" + status + '\'' +
'}';
return toStr;
}
private:
string id;//学号
string name;//姓名
string sex;//性别
string stuClass;//班级
string status;//状态(在读、休学、退学)
};
#endif // !STUDENT_CPP

17
StudentDAO.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef STUDENT_DAO_H
#define STUDENT_DAO_H
#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include<iostream>
#include<vector>
#include"Student.cpp"
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);
#endif // !STUDENT_DAO_H

60
StudentDAOImpl.cpp Normal file
View File

@ -0,0 +1,60 @@
#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);
}
Student& getStudentById(_ConnectionPtr connection, string id) {
string sql = "select * from student where id = '" + id + "'";
_RecordsetPtr record = connection->Execute(sql.c_str(), NULL, (long)0);
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);
return *student;
}
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);
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;
}

100
Test.cpp Normal file
View File

@ -0,0 +1,100 @@
/*
DAO功能是否能正常运行
*/
#if 0
#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include <stdio.h> // wprintf
#include <iostream>
#include <locale.h> // setlocale
#include <string>
#include<vector>
#include "DBCUtils.h"
#include "StudentDAO.h"
#include "CourseDAO.h"
#include "GradeDAO.h"
using namespace std;
int main() {
setlocale(LC_ALL, "chs");
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
_ConnectionPtr pconnect(_uuidof(Connection));
string strConnect = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=stuAdminSystem;Data Source=LAPTOP-4DMOD6O5";
pconnect->Open(strConnect.c_str(), "", "", NULL);
//student
/*
Student* stu = new Student("2", "李四", "", "计科00000", "休学");
addStudent(pconnect, *stu);
deleteStudent(pconnect, "4");
updateStudentClass(pconnect, "3", "软件12004");
updateStudentStatus(pconnect, "3", "休学");
Student student = getStudentById(pconnect, "1");
cout << student.toString() << "\n";
vector<Student> v = getStudentByName(pconnect, "张三");
for (int i = 0; i < v.size(); i++)
cout << v[i].toString() << "\n";
*/
//course
/*
Course* course1 = new Course("A001","高数");
Course* course2 = new Course("A002","C语言");
Course* course3 = new Course("A003","体育");
addCourse(pconnect, *course1);
addCourse(pconnect, *course2);
addCourse(pconnect, *course3);
deleteCourse(pconnect, "A002");
updateCourseName(pconnect, "A003", "物理");
Course cou = getCourseById(pconnect, "A001");
cout << cou.toString() << "\n";
vector<Course> v = getCourseByName(pconnect, "高数");
for (int i = 0; i < v.size(); i++)
cout << v[i].toString() << "\n";
*/
//grade
/*
Grade* course1 = new Grade("1","A001","95");
Grade* course2 = new Grade("1","A001-1", "95");
Grade* course3 = new Grade("1","A003", "95");
Grade* course4 = new Grade("2","A001", "95");
Grade* course5 = new Grade("2","A003", "95");
addGrade(pconnect, *course1);
addGrade(pconnect, *course2);
addGrade(pconnect, *course3);
addGrade(pconnect, *course4);
addGrade(pconnect, *course5);
deleteOnesGrade(pconnect, "1","A001-1");
deleteGradeByStuId(pconnect, "1");
deleteGradeByCourseId(pconnect, "A001");
updateOnesGrade(pconnect,"1","A001-1","98.4");
Grade gra = getOnesGrade(pconnect, "1","A001");
cout << gra.toString() << "\n";
cout << "\n";
vector<Grade> v = getGradeByStuId(pconnect, "1");
for (int i = 0; i < v.size(); i++)
cout << v[i].toString() << "\n";
cout << "\n";
vector<Grade> v1 = getGradeByCourseId(pconnect, "A001");
for (int i = 0; i < v1.size(); i++)
cout << v1[i].toString() << "\n";
*/
CoUninitialize();
return 0;
}
#endif

34
main.cpp Normal file
View File

@ -0,0 +1,34 @@
#import "c:\\Program Files\\Common Files\\System\\ado\\msado15.dll" no_namespace rename("EOF", "EndOfFile")
#include <stdio.h> // wprintf
#include <iostream>
#include <locale.h> // setlocale
#include <string>
#include<vector>
#include "DBCUtils.h"
#include "StudentDAO.h"
using namespace std;
//¸ß¼¶¹¦ÄÜ Ö±½ÓÊäÈësqlÓï¾ä
int main() {
setlocale(LC_ALL, "chs");
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
string pwd;
wprintf(L"Enter password:");
getline(cin, pwd);
_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());
}
}
CoUninitialize();
return 0;
}