是数据库的课设,自己写完后就发上来了
先放个思维导图
E-R图也放一个
通过ODBC连接SQL Server数据库,实现了在命令行内执行订餐系统的管理
注意!!:数据库中的中文数据输出正常,但是无法读取命令行中输入的中文字符(读取到的是乱码,应该是没有转换字符导致),请尽量使用英文 ; 且在命令行中有概率出现自动换行,但是不影响使用
!!必看!!:将代码中 连接 块的 TEST 换成ODBC数据源的名字, UserName 换成SQL Server的登录名, PassWord 换成登录名的密码(SQL Server不支持太弱的密码远程登录,请至少包含英文小写字符和数字且不小于8位)
环境:VS2022,Mircosoft SQLserver2019,Windows10 最新的SDK,在ODBC数据源中已将OrderingSystem设为默认数据库
直接放功能代码和运行图吧
目录
1.使用ODBC连接SQL Server数据库:
2.主界面
3.功能实现顾客方面
(1)有注册与登录两个选项
(2)注册
(3)登录
(4)点单
(5)查询订单
(6)注销用户
4.功能实现管理员方面
(1)登录
(2)以对顾客增删查改为例
添加顾客信息
删除顾客信息
查询顾客信息
修改顾客信息
1.使用ODBC连接SQL Server数据库:
void Link()
{
ret = SQLAllocHandle(SQL_HANDLE_ENV, NULL, &henv);//申请环境句柄
ret = SQLSetEnvAttr(henv,SQL_ATTR_ODBC_VERSION,(SQLPOINTER)SQL_OV_ODBC3,SQL_IS_INTEGER);
ret = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);//申请数据库连接句柄
ret = SQLConnect(hdbc, (SQLWCHAR*)L"TEST", SQL_NTS, (SQLWCHAR*)L"UserName", SQL_NTS, (SQLWCHAR*)L"PassWord", SQL_NTS);
if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)) {
cout << "Failed to connect to database!" << endl << endl;
}
else {
cout << "Database connection succeeded!" << endl << endl;
}
}
2.主界面
3.功能实现顾客方面
(1)有注册与登录两个选项
(2)注册
void Insert_Customer() {
//sql语句:INSERT INTO customer VALUES ('John','D7-209',123456,124)
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
string str1 = "INSERT INTO customer VALUES ('";
cout << "Please enter your name, address, telephone number and password in sequence." << endl;
string cname, cplace,ctel,ccode;
cin >> cname >> cplace >> ctel >> ccode;
string str2 = str1 + cname + "','" + cplace + "'," + ctel +"," + ccode + ")";
wchar_t* wc = new wchar_t[str2.size()];
swprintf(wc, 100, L"%S", str2.c_str());
ret = SQLExecDirect(hstmt, wc, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
cout << "User added successfully!" << endl;
}
else {
cout << "Failed to add user! Or user phone number already exists." << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
}
free();
cout << endl << endl;
}
接下来可以继续进行顾客的操作
(3)登录
void CustomerLogin() {
//SQL:SELECt * FROM customer WHERe ctel = 12345 AND ccode = 123
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
string str01 = "SELECt * FROM customer WHERe ctel = ";
cout << "Please enter the customer tel and password." << endl;
string ctel, ccode;
cin >> ctel >> ccode;
string str02 = str01 + ctel + " AND ccode = " + ccode;
wchar_t* wc = new wchar_t[str02.size()];
swprintf(wc, 100, L"%S", str02.c_str());
ret = SQLExecDirect(hstmt, wc, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO && ret != NULL) {
SQLCHAR str1[50], str2[50], str3[50], str4[50];
SQLLEN len_str1, len_str2, len_str3, len_str4;
while (SQLFetch(hstmt) != SQL_NO_DATA)
{
SQLGetData(hstmt, 1, SQL_C_CHAR, str1, sizeof(str1), &len_str1);
SQLGetData(hstmt, 2, SQL_C_CHAR, str2, sizeof(str2), &len_str2);
SQLGetData(hstmt, 3, SQL_C_CHAR, str3, sizeof(str3), &len_str3);
SQLGetData(hstmt, 4, SQL_C_CHAR, str4, sizeof(str4), &len_str4);
}
if ((const char*)str3 == ctel && (const char*)str4 == ccode) {
cout << "Login succeeded!" << endl << endl;
free();
Action_Customer();
}
else {
cout << "No such person found!" << endl;
CustomerLogin();
}
}
else {
cout << "Login failed!" << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
CustomerLogin();
}
free();
}
使用在数据库中录入的信息登录
1是注销 2是点单 3是查询订单 0是返回主界面
(4)点单
void Ordering() {
//sql语句:INSERT INTO orders ( pid, money, ctel ) VALUES (1,18,12345)
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
cout << "Please enter the product id and telephone number in sequence." << endl;
string pid ,ctel;
cin >> pid >> ctel;
string sql1 = "SELECt pprice FROM products WHERe pid = ";
string sql2 = sql1 + pid;
wchar_t* wc_t = new wchar_t[sql2.size()];
swprintf(wc_t, 100, L"%S", sql2.c_str());
ret = SQLExecDirect(hstmt, wc_t, SQL_NTS);
SQLCHAR str[50];
SQLLEN len_str;
while (SQLFetch(hstmt) != SQL_NO_DATA)
{
SQLGetData(hstmt, 1, SQL_C_CHAR, str, sizeof(str), &len_str);
}
string money((const char*)str);
cout << money << endl;
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
string str1 = "INSERT INTO orders ( pid, money, ctel ) VALUES (";
string str2 = str1 + pid + "," + money + "," + ctel + ")";
wchar_t* wc1 = new wchar_t[str2.size()];
swprintf(wc1, 100, L"%S", str2.c_str());
ret = SQLExecDirect(hstmt, wc1, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
cout << "Order successfully!" << endl;
}
else {
cout << "Order failed!" << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
}
free();
cout << endl << endl;
Action_Customer();
}
(5)查询订单
void Inquire_Order_Customer() {
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
string str1 = "SELECt * FROM orders WHERe ctel = ";
cout << "Please enter the telephone number." << endl;
string ctel;
cin >> ctel;
string str2 = str1 + ctel ;
wchar_t* wc = new wchar_t[str2.size()];
swprintf(wc, 100, L"%S", str2.c_str());
ret = SQLExecDirect(hstmt, wc, SQL_NTS);
cout << "OrderNumber" << " " << "OrderName" << " " << "Amount" << " " << "TelNumber" << endl << endl;
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
SQLCHAR str1[50], str2[50], str3[50], str4[50];
SQLLEN len_str1, len_str2, len_str3, len_str4;
while (SQLFetch(hstmt) != SQL_NO_DATA)
{
SQLGetData(hstmt, 1, SQL_C_CHAR, str1, sizeof(str1), &len_str1);
SQLGetData(hstmt, 2, SQL_C_CHAR, str2, sizeof(str2), &len_str2);
SQLGetData(hstmt, 3, SQL_C_CHAR, str3, sizeof(str3), &len_str3);
SQLGetData(hstmt, 4, SQL_C_CHAR, str4, sizeof(str4), &len_str4);
cout << str1 << " " << str2 << " " << str3 << " " << str4 << endl;
}
}
else {
cout << "Query failed!" << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
}
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
free();
cout << endl << endl;
Action_Customer();
}
由于我们在建表时设置了让onum从101开始以1的大小自增,所以订单编号会主动增加
(6)注销用户
void Delete_Customer()
{
//sql语句:DELETE FROM customer WHERe ctel ='12345'
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
string str1 = "DELETe FROM customer WHERe ctel ='";
cout << "Please enter your mobile number." << endl;
string ctel;
cin >> ctel;
string str2 = str1 + ctel + "'";
wchar_t* wc = new wchar_t[str2.size()];
swprintf(wc, 100, L"%S", str2.c_str());
ret = SQLExecDirect(hstmt, wc, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
cout << "Logout succeeded!" << endl;
}
else {
cout << "Logout failed!" << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
}
free();
cout << endl << endl;
}
4.功能实现管理员方面
(1)登录
void AdminLogin() {
//SQL:SELECt * FROM admin WHERe anum = 101 AND acode = 123
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
string str01 = "SELECt * FROM admin WHERe anum = ";
cout << "Please enter the administrator number and password." << endl;
string anum, acode;
cin >> anum >> acode;
string str02 = str01 + anum + " AND acode = " + acode;
wchar_t* wc = new wchar_t[str02.size()];
swprintf(wc, 100, L"%S", str02.c_str());
ret = SQLExecDirect(hstmt, wc, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
SQLCHAR str1[50], str2[50];
SQLLEN len_str1, len_str2;
while (SQLFetch(hstmt) != SQL_NO_DATA)
{
SQLGetData(hstmt, 1, SQL_C_CHAR, str1, sizeof(str1), &len_str1);
SQLGetData(hstmt, 2, SQL_C_CHAR, str2, sizeof(str2), &len_str2);
}
if ((const char*)str1 == anum && (const char*)str2 == acode) {
cout << "Login succeeded!" << endl << endl;
free();
Action_Admin();
}
else {
cout << "No such person found!" << endl;
AdminLogin();
}
}
else {
cout << "Login failed!" << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
AdminLogin();
}
free();
}
void AdminLogin() {
//SQL:SELECt * FROM admin WHERe anum = 101 AND acode = 123
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
string str01 = "SELECt * FROM admin WHERe anum = ";
cout << "Please enter the administrator number and password." << endl;
string anum, acode;
cin >> anum >> acode;
string str02 = str01 + anum + " AND acode = " + acode;
wchar_t* wc = new wchar_t[str02.size()];
swprintf(wc, 100, L"%S", str02.c_str());
ret = SQLExecDirect(hstmt, wc, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
SQLCHAR str1[50], str2[50];
SQLLEN len_str1, len_str2;
while (SQLFetch(hstmt) != SQL_NO_DATA)
{
SQLGetData(hstmt, 1, SQL_C_CHAR, str1, sizeof(str1), &len_str1);
SQLGetData(hstmt, 2, SQL_C_CHAR, str2, sizeof(str2), &len_str2);
}
if ((const char*)str1 == anum && (const char*)str2 == acode) {
cout << "Login succeeded!" << endl << endl;
free();
Action_Admin();
}
else {
cout << "No such person found!" << endl;
AdminLogin();
}
}
else {
cout << "Login failed!" << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
AdminLogin();
}
free();
}
1是对管理员增删查改
2是对顾客增删查改
3是对订单增删查改
4是对商品增删查改
(2)以对顾客增删查改为例
1是添加顾客信息 2是删除顾客信息 3是查询顾客信息 4是修改顾客信息
-
添加顾客信息
Insert_Customer(); 该函数与上面的顾客注册函数一致
Action_Admin(); 该函数显示管理员操作菜单
-
删除顾客信息
Delete_Customer(); 该函数与上面的顾客注销函数一致
Action_Admin(); 该函数显示管理员操作菜单
-
查询顾客信息
void Inquire_Customer() {
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
SQLWCHAR sql1[] = L"SELECT * FROM customer";
ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
SQLCHAR str1[50], str2[50], str3[50], str4[50];
SQLLEN len_str1, len_str2, len_str3, len_str4;
cout << "Name" << " " << "Address" << " " << "TelNumber" << " " << "Password" << endl << endl;
while (SQLFetch(hstmt) != SQL_NO_DATA)
{
SQLGetData(hstmt, 1, SQL_C_CHAR, str1, sizeof(str1), &len_str1);
SQLGetData(hstmt, 2, SQL_C_CHAR, str2, sizeof(str2), &len_str2);
SQLGetData(hstmt, 3, SQL_C_CHAR, str3, sizeof(str3), &len_str3);
SQLGetData(hstmt, 4, SQL_C_CHAR, str4, sizeof(str4), &len_str4);
cout << str1 << " " << str2 << " " << str3 << " " << str4 << endl;
}
}
else {
cout << "Query failed!" << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
}
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
free();
cout << endl << endl;
Action_Admin();
}
-
修改顾客信息
void Change_Customer() {
//sql语句:UPDATE customer SET cname = '李阳', cplace = 'D8-103', ccode = 5678 WHERe ctel = 56789
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
string str1 = "UPDATE customer SET cname = '";
cout << "Please enter your name, address, password and telephone number in sequence. (The telephone number cannot be modified, please register separately)" << endl;
string cname, cplace, ccode, ctel;
cin >> cname >> cplace >> ccode >> ctel;
string str2 = str1 + cname + "', cplace = '" + cplace + "', ccode =" + ccode + " WHERe ctel = " + ctel;
wchar_t* wc = new wchar_t[str2.size()];
swprintf(wc, 100, L"%S", str2.c_str());
ret = SQLExecDirect(hstmt, wc, SQL_NTS);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
cout << "User modification succeeded!" << endl;
}
else {
cout << "User modification failed!" << endl;
handleResult(hstmt, SQL_HANDLE_STMT, ret);
error(ret);
}
free();
cout << endl << endl;
Action_Admin();
}
以此为例,其余的对于管理员、订单、商品增删查改的函数和操作都基本一致。
MSSQL建库的代码就不放了,大差不差的,有需要就留言吧
全部源码放在了GitHub上,如果觉得有用可以去点个star哦
GitHub - MagicNeko913/ODBC-OrderingSystem-



