#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<sql.h>
#include<sqlext.h>
#include<sqltypes.h>
#include<time.h>
SQLRETURN ret;
SQLHENV henv;
SQLHDBC hdbc;
SQLHSTMT hstmt;
void showMenu() {
system("cls");
printf("==============================================================\n");
printf("* *\n");
printf("* -------------------------- *\n");
printf("* {欢迎使用图书管理系统} *\n");
printf("* -------------------------- *\n");
printf("* *\n");
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");
printf("* *\n");
printf("==============================================================\n");
printf("请输入编号:");
}
void Connect() {
const char* SY1 = L"BMS";
unsigned char* SY = (unsigned char*)SY1;
const char* db21 = L"sa";
unsigned char* db2 = (unsigned char*)db21;
const char* pass1 = L"";
unsigned char* pass = (unsigned char*)pass1;
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
ret = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, SQL_IS_INTEGER);
ret = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
ret = SQLConnect(hdbc, SY, SQL_NTS, db2, SQL_NTS, pass, SQL_NTS);
if (!(ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)) {
printf("连接数据库失败!\n");
return;
}
ret = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
}
void free() {
SQLDisconnect(hdbc);
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
SQLFreeHandle(SQL_HANDLE_ENV, henv);
}
void timer(char* str) {
time_t timep;
struct tm* p;
time(&timep);
p = gmtime(&timep);
int year, month, day;
year = 1900 + p->tm_year;
month = 1 + p->tm_mon;
day = p->tm_mday;
for (int j = 0; j < 11; j++)
str[j] = '0';
int i = 3;
while (year) {
str[i] = year % 10 + '0';
year /= 10;
i--;
}
str[4] = '-';
i = 6;
while (month) {
str[i] = month % 10 + '0';
month /= 10;
i--;
}
str[7] = '-';
i = 9;
while (day) {
str[i] = day % 10 + '0';
day /= 10;
i--;
}
str[10] = '\0';
}
void showAll() {
Connect();
SQLPrepare(hstmt, (SQLWCHAR*)(L"select * from BMS.dbo.system_book"), SQL_NTS);
ret = SQLExecute(hstmt);
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO)
{
SQLCHAR str1[10], str2[30], str3[30], str4[30], str5[30], str6[30], str7[30], str8[5];
SQLINTEGER len_str1, len_str2, len_str3, len_str4, len_str5, len_str6, len_str7, len_str8;
printf("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("%-10s%-20s%-20s%-30s%-30s%-20s%-20s%-15s\n", "书籍编号", "书籍名称", "书籍类别", "书籍作者", "出版社名称", "出版日期", "登记日期", "是否被借出");
printf("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
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);
SQLGetData(hstmt, 6, SQL_C_CHAR, str6, 50, &len_str6);
SQLGetData(hstmt, 7, SQL_C_CHAR, str7, 50, &len_str7);
SQLGetData(hstmt, 8, SQL_C_CHAR, str8, 50, &len_str8);
printf("%-10s%-20s%-20s%-30s%-30s%-20s%-20s%-15s\n", str1, str2, str3, str4, str5, str6, str7, str8);
}
printf("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
}
free();
}
void findBook() {
char field[50];
int database_score, c_score;
Connect();
printf("=======================================================\n");
printf("* [1]. 按书籍编号 *\n");
printf("* [2]. 按书籍名称 *\n");
printf("* [3]. 按书籍类别 *\n");
printf("* [4]. 按书籍作者 *\n");
printf("* [5]. 按出版社名称 *\n");
printf("* [0]. 退出此次操作 *\n");
printf("=======================================================\n");
int flag = 1, flag1 = 1, flag2 = 1;
char ch, temp;
printf("请输入编号:");
while (flag) {
scanf_s("%c", &ch);
flag2 = 1;
while (temp = getchar()) {
if (temp != '\n')
flag2 = 0;
if (temp == '\n')
break;
}
if (flag2) {
switch (ch) {
case '1': {
flag = 0;
printf("请输入书籍编号\n");
SQLPrepare(hstmt, (SQLWCHAR*)(L"select * from BMS.dbo.system_book where b_id like ?"), SQL_NTS);
break;
}
case '2': {
flag = 0;
printf("请输入书籍名称\n");
SQLPrepare(hstmt, (SQLWCHAR*)(L"select * from BMS.dbo.system_book where b_name like ?"), SQL_NTS);
break;
}
case '3': {
flag = 0;
printf("请输入书籍类别\n");
SQLPrepare(hstmt, (SQLWCHAR*)(L"select * from BMS.dbo.system_book where book_style like ?"), SQL_NTS);
break;
}
case '4': {
flag = 0;
printf("请输入书籍作者\n");
SQLPrepare(hstmt, (SQLWCHAR*)(L"select * from BMS.dbo.system_book where b_author like ?"), SQL_NTS);
break;
}
case '5': {
flag = 0;
printf("请输入出版社名称\n");
SQLPrepare(hstmt, (SQLWCHAR*)(L"select * from BMS.dbo.system_book where b_pub like ?"), SQL_NTS);
break;
}
case '0':
flag = 0;
flag1 = 0;
break;
default:
flag2 = 0;
break;
}
}
if (flag2 == 0)
printf("输入有误,请重新输入!\n");
}
if (flag1) {
scanf("%[^\n]", field);
getchar();
char fieldF[50] = "%", fieldT[2] = "%";
strcat(fieldF, field);
strcat(fieldF, fieldT);
SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_CHAR, strlen(fieldF), 0, &fieldF, 100, NULL);
ret = SQLExecute(hstmt);
if (ret == SQL_SUCCESS)
{
SQLCHAR str1[10], str2[30], str3[30], str4[30], str5[30], str6[30], str7[30], str8[5];
SQLINTEGER len_str1, len_str2, len_str3, len_str4, len_str5, len_str6, len_str7, len_str8;
printf("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
printf("%-10s%-20s%-20s%-30s%-30s%-30s%-20s%-15s\n", "书籍编号", "书籍名称", "书籍类别", "书籍作者", "出版社名称", "出版日期", "登记日期", "是否被借出");
printf("--------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n");
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,