Bootstrap

宿舍管理项目(大一C/C++实践)ODBC连接

注:这里的项目会用到VS连接SQL Sever数据库,若在此不明白的朋友请先看看前一篇文章,2024-11-30 01:45:17发布,这篇相当于上一篇文章的实践。

一定!!!一定!!!一定!!!要看上一篇文章,因为ODBC连接方法在上一篇中做了讲解

因为大一朋友应该还没有系统学习数据库,故在这里我们创建的数据库不使用任何外键约束,且仅用单表存储数据。

数据库创建

1.打开SQL Sever并使用你的最高权限账户登录(SQL默认的是sa

2.新建查询表,并写入以下命令

create database stu_db;

点击执行

use stu_db;

create table test(
学号 varchar(20) not null primary key,
姓名 char(20) not null,
楼栋号 tinyint not null,
寝室号 smallint not null,
是否回寝 tinyint
);

点击执行

3.检查(可选)

若正常出现结果应和上图一样。

C/C++代码编写

这里作者使用的是VS

1.固定连接语句(这一段代码非常死板,不用理解,只需要知道更改哪里怎么更改就行)

虽然这一段看着多,但是真正要改的就只有一行,这里只是让大家先看看要改哪里,不用复制,我后面也会附上完整项目代码。

SQLRETURN ret;
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;

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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
/*数据为配置的ODBC数据源名称,sa为该配置下的SQL账户比如我就直接留的是sa,123456为
sa账户的密码,这里根据自己的配置进行修改,要改!!!*/
if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
{
	printf("连接数据库失败!\n");
}

ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
/*执行sql语句*/
SQLCHAR sql1[] = "use stu_db";
SQLCHAR sql2[] = "select * from test";
ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
ret = SQLExecDirect(hstmt, sql2, SQL_NTS);
if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	printf("未调用到正确的数据库\n");
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
	printf("学号\t\t姓名\t楼栋号\t寝室号\t是否回寝\n");
	SQLCHAR str1[50], str2[50], str3[50], str4[50], str5[50], str6[50];
	SQLLEN len_str1, len_str2, len_str3, len_str4, len_str5, len_str6;
	while (SQLFetch(hstmt) != SQL_NO_DATA)
	{
		SQLGetData(hstmt, 1, SQL_C_CHAR, str1, 50, &len_str1);   //获取第一列数据
		SQLGetData(hstmt, 2, SQL_C_CHAR, str2, 50, &len_str2);
		SQLGetData(hstmt, 3, SQL_C_CHAR, str3, 50, &len_str3);
		SQLGetData(hstmt, 4, SQL_C_CHAR, str4, 50, &len_str4);
		SQLGetData(hstmt, 5, SQL_C_CHAR, str5, 50, &len_str5);
		printf("%s\t%s\t%s\t%s\t%s\n", str1, str2, str3, str4, str5);
	}
}

SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄,不用理解
SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄,不用理解

2.//sever.h

#pragma once
#ifndef sever.h

#include<stdio.h>
#include<windows.h>
#include<sql.h>
#include<sqlext.h>
#include<sqltypes.h>

void query_all();
void query_where();
void query_ID(char* id);
void query_BN(int building_number);
void query_RN(int room_number);

bool function(char* id);//查询是否存在已有学号
void add_width(char* id, char* name, int build, int room, int k);
void add_length(char* name_, char* id_);

void modify(char* id, char* name, int build, int room, int k);

void delete_all(char* id);

struct student
{
	char id[50];
	char name[50];
	int building;
	int room;
	int k;
};
typedef student STU;

#endif // !sever.h

3.sever.cpp

#define _CRT_SECURE_NO_WARNINGS

#include"sever.h"
#include"menu.h"

void query_all()
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*data_test为配置的ODBC数据源名称,这里根据自己的配置进行修改*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test";
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, sql2, SQL_NTS);
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
		printf("未调用到正确的数据库\n");
	if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
	{
		printf("学号\t\t姓名\t楼栋号\t寝室号\t是否回寝\n");
		SQLCHAR str1[50], str2[50], str3[50], str4[50], str5[50], str6[50];
		SQLLEN len_str1, len_str2, len_str3, len_str4, len_str5, len_str6;
		while (SQLFetch(hstmt) != SQL_NO_DATA)
		{
			SQLGetData(hstmt, 1, SQL_C_CHAR, str1, 50, &len_str1);   //获取第一列数据
			SQLGetData(hstmt, 2, SQL_C_CHAR, str2, 50, &len_str2);
			SQLGetData(hstmt, 3, SQL_C_CHAR, str3, 50, &len_str3);
			SQLGetData(hstmt, 4, SQL_C_CHAR, str4, 50, &len_str4);
			SQLGetData(hstmt, 5, SQL_C_CHAR, str5, 50, &len_str5);
			printf("%s\t%s\t%s\t%s\t%s\n", str1, str2, str3, str4, str5);
		}
	}

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

void query_where()
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*data_test为配置的ODBC数据源名称,这里根据自己的配置进行修改*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, sql2, SQL_NTS);
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
		printf("无外出同学\n");
	if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
	{
		printf("学号\t\t姓名\t楼栋号\t寝室号\t是否回寝\n");
		SQLCHAR str1[50], str2[50], str3[50], str4[50], str5[50], str6[50];
		SQLLEN len_str1, len_str2, len_str3, len_str4, len_str5, len_str6;
		while (SQLFetch(hstmt) != SQL_NO_DATA)
		{
			SQLGetData(hstmt, 1, SQL_C_CHAR, str1, 50, &len_str1);   //获取第一列数据
			SQLGetData(hstmt, 2, SQL_C_CHAR, str2, 50, &len_str2);
			SQLGetData(hstmt, 3, SQL_C_CHAR, str3, 50, &len_str3);
			SQLGetData(hstmt, 4, SQL_C_CHAR, str4, 50, &len_str4);
			SQLGetData(hstmt, 5, SQL_C_CHAR, str5, 50, &len_str5);
			printf("%s\t%s\t%s\t%s\t%s\n", str1, str2, str3, str4, str5);
		}
	}

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

void query_ID(char* id)
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*data_test为配置的ODBC数据源名称,这里根据自己的配置进行修改*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	char str[100];
	sprintf(str, "select * from test where 学号='%s'", id);
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, (SQLCHAR*)str, SQL_NTS);
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
		printf("未录入该同学信息\n");
	if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
	{
		printf("学号\t\t姓名\t楼栋号\t寝室号\t是否回寝\n");
		SQLCHAR str1[50], str2[50], str3[50], str4[50], str5[50], str6[50];
		SQLLEN len_str1, len_str2, len_str3, len_str4, len_str5, len_str6;
		while (SQLFetch(hstmt) != SQL_NO_DATA)
		{
			SQLGetData(hstmt, 1, SQL_C_CHAR, str1, 50, &len_str1);   //获取第一列数据
			SQLGetData(hstmt, 2, SQL_C_CHAR, str2, 50, &len_str2);
			SQLGetData(hstmt, 3, SQL_C_CHAR, str3, 50, &len_str3);
			SQLGetData(hstmt, 4, SQL_C_CHAR, str4, 50, &len_str4);
			SQLGetData(hstmt, 5, SQL_C_CHAR, str5, 50, &len_str5);
			printf("%s\t%s\t%s\t%s\t%s\n", str1, str2, str3, str4, str5);
		}
	}

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

void query_BN(int building_number)
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*data_test为配置的ODBC数据源名称,这里根据自己的配置进行修改*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	char str[100];
	sprintf(str, "select * from test where 楼栋号=%d", building_number);
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, (SQLCHAR*)str, SQL_NTS);
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
		printf("未录入该同学信息\n");
	if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
	{
		printf("学号\t\t姓名\t楼栋号\t寝室号\t是否回寝\n");
		SQLCHAR str1[50], str2[50], str3[50], str4[50], str5[50], str6[50];
		SQLLEN len_str1, len_str2, len_str3, len_str4, len_str5, len_str6;
		while (SQLFetch(hstmt) != SQL_NO_DATA)
		{
			SQLGetData(hstmt, 1, SQL_C_CHAR, str1, 50, &len_str1);   //获取第一列数据
			SQLGetData(hstmt, 2, SQL_C_CHAR, str2, 50, &len_str2);
			SQLGetData(hstmt, 3, SQL_C_CHAR, str3, 50, &len_str3);
			SQLGetData(hstmt, 4, SQL_C_CHAR, str4, 50, &len_str4);
			SQLGetData(hstmt, 5, SQL_C_CHAR, str5, 50, &len_str5);
			printf("%s\t%s\t%s\t%s\t%s\n", str1, str2, str3, str4, str5);
		}
	}

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

void query_RN(int room_number)
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*OBDC数据库命名*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	char str[100];
	sprintf(str, "select * from test where 寝室号=%d", room_number);
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, (SQLCHAR*)str, SQL_NTS);
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
		printf("未录入该同学信息\n");
	if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
	{
		printf("学号\t\t姓名\t楼栋号\t寝室号\t是否回寝\n");
		SQLCHAR str1[50], str2[50], str3[50], str4[50], str5[50], str6[50];
		SQLLEN len_str1, len_str2, len_str3, len_str4, len_str5, len_str6;
		while (SQLFetch(hstmt) != SQL_NO_DATA)
		{
			SQLGetData(hstmt, 1, SQL_C_CHAR, str1, 50, &len_str1);   //获取第一列数据
			SQLGetData(hstmt, 2, SQL_C_CHAR, str2, 50, &len_str2);
			SQLGetData(hstmt, 3, SQL_C_CHAR, str3, 50, &len_str3);
			SQLGetData(hstmt, 4, SQL_C_CHAR, str4, 50, &len_str4);
			SQLGetData(hstmt, 5, SQL_C_CHAR, str5, 50, &len_str5);
			printf("%s\t%s\t%s\t%s\t%s\n", str1, str2, str3, str4, str5);
		}
	}

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

bool function(char* id)
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*data_test为配置的ODBC数据源名称,这里根据自己的配置进行修改*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	char str[100];
	sprintf(str, "select * from test where 学号='%s'", id);
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, (SQLCHAR*)str, SQL_NTS);
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
		printf("error\n");
	if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
	{
		if (SQLFetch(hstmt) == SQL_NO_DATA)
		{
			return false;
		}
		else
		{
			query_ID(id);
		}
	}

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
	return true;
}

void add_width(char* id,char* name,int build,int room,int k)
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*OBDC数据库命名*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	char str[400];
	sprintf(str, "insert into test(学号,姓名,楼栋号,寝室号,是否回寝)values('%s','%s',%d,%d,%d)", id,name,build,room,k);
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, (SQLCHAR*)str, SQL_NTS);

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

void add_length(char* name_, char* id_)
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*OBDC数据库命名*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	char str[400];
	sprintf(str, "alter table test add %s %s", name_, id_);
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, (SQLCHAR*)str, SQL_NTS);

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

void modify(char* id, char* name, int build, int room, int k)
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*OBDC数据库命名*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	char str[400];
	sprintf(str, "update test set 姓名='%s', 楼栋号=%d, 寝室号=%d, 是否回寝=%d where 学号='%s'", name, build, room, k, id);
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, (SQLCHAR*)str, SQL_NTS);

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

void delete_all(char* id)
{
	SQLRETURN ret;
	SQLHENV henv;
	SQLHDBC hdbc;
	SQLHSTMT hstmt;

	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, (SQLCHAR*)"数据", SQL_NTS, (SQLCHAR*)"sa", SQL_NTS, (SQLCHAR*)"123456", SQL_NTS);
	/*OBDC数据库命名*/
	if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO))
	{
		printf("连接数据库失败!\n");
	}

	ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
	/*执行sql语句*/
	SQLCHAR sql1[] = "use stu_db";
	SQLCHAR sql2[] = "select * from test where 是否回寝=0";
	char str[400];
	sprintf(str, "delete from test where 学号='%s'", id);
	ret = SQLExecDirect(hstmt, sql1, SQL_NTS);
	ret = SQLExecDirect(hstmt, (SQLCHAR*)str, SQL_NTS);

	SQLFreeHandle(SQL_HANDLE_DBC, hdbc);//释放连接句柄
	SQLFreeHandle(SQL_HANDLE_ENV, henv);//释放环境句柄
}

4.menu.h 

#pragma once
#ifndef menu.h

#include<stdio.h>
#include<iostream>
#include<Windows.h>
#include<stdlib.h>
#include<string.h>

void menu_1();
int choose_1();
void menu_one();
void menu_two();
void num_1(int count);
void num_2(int count);
void menu_3_();
void num_3(int count);
void num_4(int count);
void scanf_();
void menu_three();
void num_5(int count);
void menu_four();
void num_6(int count);

void ClearBuffer();

#endif // !menu.h

5.menu.cpp 

#define _CRT_SECURE_NO_WARNINGS

#include"menu.h"
#include"sever.h"

void menu_1()
{
	std::cout << "\t\t后台管理系统\n";
	std::cout << "\t1:查询\n";
	std::cout << "\t2:增加\n";
	std::cout << "\t3:修改\n";
	std::cout << "\t4:删除\n";
}

int choose_1()
{
	int count = 0;
	std::cout << "\t请选择:";
	std::cin >> count;
	switch (count)
	{
	case 1:return 1; break;
	case 2:return 2; break;
	case 3:return 3; break;
	case 4:return 4; break;
	default:
		break;
	}
}

void menu_one()
{
	std::cout << "\t1:直接查询全部数据\n";
	std::cout << "\t2:仅查询未回寝\n";
	std::cout << "\t3:查询某项数据\n";
}

void menu_two()
{
	std::cout << "\t1:增加行\n";
	std::cout << "\t2:增加列\n";
}

void num_1(int count)
{
	switch (count)
	{
	case 1:menu_one(); break;
	case 2:menu_two(); break;
	case 3:menu_three(); break;
	case 4:menu_four(); break;
	}
}

void num_2(int count)
{
	switch (count)
	{
	case 1:system("cls"); query_all(); break;
	case 2:system("cls"); query_where(); break;
	case 3:
		menu_3_();
		int count1 = choose_1();
		num_3(count1);
	}
}

void menu_3_()
{
	std::cout << "\t\t1:以学号\n";
	std::cout << "\t\t2:以楼栋号\n";
	std::cout << "\t\t3:以寝室号\n";
}

void num_3(int count)
{
	switch (count)
	{
	case 1:
	{
		system("cls");
		char id[1000];
		getchar();
		gets_s(id);
		query_ID(id); break;
	}
	case 2:
	{
		system("cls");
		int building_number=0;
		scanf_s("%d", &building_number);
		query_BN(building_number); break;
	}
	case 3:
	{
		system("cls");
		int room_number = 0;
		scanf_s("%d", &room_number);
		query_RN(room_number); break;
	}
	}
}

void num_4(int count)
{
	switch (count)
	{
	case 1:
	{
		system("cls");
		std::cout << "请输入您所需要添加的学号\n";
		std::cout << "请注意:此处输入学号后并非代表添加完成,这是因为需要根据您所输入的学号判断该同学是否已经存在\n";
		char id[1000];
		getchar();
		gets_s(id);
		int fun = 0;
		fun=function(id);
		if (fun == 0)
		{
			int k = 1;
			while (1)
			{
				STU S;
				std::cout << "请输入同学姓名:";
				scanf_s("%s", S.name);
				std::cout << "请输入同学楼栋号:";
				scanf_s("%d", &S.building);
				std::cout << "请输入同学的寝室号:";
				scanf_s("%d", &S.room);
				std::cout << "该同学是否回寝室(1:是 0:否):";
				scanf_s("%d", &S.k);
				
				add_width(id, S.name, S.building, S.room, k);

				std::cout << "需要重复输入?(0:结束输入 1:继续输入)";
				std::cin >> k;
				if (k != 1)
					exit(0);
			}
		}
		else
		{
			std::cout << "请问您是否是想改变该同学的数据?";
		}
	}
	case 2:
	{
		ClearBuffer();
		char name_[200];
		char id_[80];
		std::cout << "请输入您所需要的名称:";
		gets_s(name_);
		//ClearBuffer();
		std::cout << "以下可以选择常用类型,或者您也可以选择0来使用自行输入\n";
		std::cout << "1:int\t2:tinint\t3:smallint\t4:bigint\n5:char()\t6:varchar()\n";
		std::cout << "请选择您所需要的类型:";
		int count = 0;
		std::cin >> count;
		switch (count)
		{
		case 0:
		{
			std::cout << "请输入:";
			ClearBuffer();
			gets_s(id_);
			break;
		}
		case 1:
		{
			sprintf(id_, "int"); break;
		}
		case 2:
		{
			sprintf(id_, "tinint"); break;
		}
		case 3:
		{
			sprintf(id_, "smallint"); break;
		}
		case 4:
		{
			sprintf(id_, "bigint"); break;
		}
		case 5:
		{
			int leng = 0;
			std::cout << "请问定长是:";
			std::cin >> leng;
			sprintf(id_, "char(%d)",leng);
			break;
		}
		case 6:
		{
			int leng = 0;
			std::cout << "请问变长是:";
			std::cin >> leng;
			sprintf(id_, "varchar(%d)", leng);
			break;
		}
		}
		add_length(name_, id_);
	}
	}
}

void menu_three()
{
	std::cout << "\t1:使用学号查询并修改\n";
}

void num_5(int count)
{
	system("cls");
	std::cout << "请注意:您必须通过学号进行修改,因为这是主键\n";
	std::cout << "请输入您所需要修改的同学的学号:";
	char str[60];
	ClearBuffer();
	gets_s(str);
	int fun = 0;
	fun=function(str);
	if (fun == 0)
	{
		std::cout << "未录入过此同学的信息\n";
		std::cout << "请注意:因为是主键查找,所以不存在多项导致程序错误的情况";
	}
	else
	{
		std::cout << "\t\n我们需要再次确认您是否需要修改此同学的信息(0:否 1:是)\n";
		std::cout << "请注意:因为学号是主键,所以无法修改学号\n\t";
		int fun_;
		std::cin >> fun_;
		if (fun_ == 1)
		{
			char name[200];
			int build, room, k;
			ClearBuffer();
			std::cout << "请输入修改后的姓名:";
			gets_s(name);
			std::cout << "请输入修改后的楼栋号:";
			std::cin >> build;
			std::cout << "请输入修改后的寝室号:";
			std::cin >> room;
			std::cout << "请输入现在是否回寝(1:是 0:否):";
			std::cin >> k;
			modify(str, name, build, room, k);
			query_ID(str);
		}
	}
}

void menu_four()
{
	std::cout << "请注意:删除操作结束后数据不可逆,我们需要了解此操作您是否是您的真实意图\n";
	std::cout << "1:继续删除操作\n";
}

void num_6(int count)
{
	system("cls");
	std::cout << "请输入您所需要删除的同学的信息";
	char id[80];
	ClearBuffer();
	gets_s(id);
	int fun = 0;
	fun = function(id);
	if (fun == 0)
	{
		std::cout << "未录入过此同学的信息\n";
		std::cout << "请注意:因为是主键查找,所以不存在多项导致程序错误的情况";
	}
	else
	{
		std::cout << "再次提醒:删除操作不可逆,是否继续(1:是 0:否):";
		int fun;
		std::cin >> fun;
		if (fun == 1)
		{
			delete_all(id);
			std::cout << "操作已完成,若不慎操作可在关闭控制台前记录删除前的所有数据";
		}
		else
		{
			std::cout << "操作中断";
		}
	}
}

void ClearBuffer()
{
	char temp;
	while ((temp = getchar()) != '\n');
}

6.main.cpp

#include"menu.h"
#include"sever.h"

int main()
{
	menu_1();
	int count1 = choose_1();
	num_1(count1);
	int count2 = choose_1();
	switch (count1)
	{
	case 1:num_2(count2); break;
	case 2:num_4(count2); break;
	case 3:num_5(count2); break;
	case 4:num_6(count2); break;
	}
	
}

7.检查(可选)

最终项目所需文件必须包含上述5个

使用注意

此时各位朋友的数据库中没有任何数据,因此先不要使用查询功能,先使用增添行来增加一些数据再进行查找。

;