基于C#的图书借阅管理系统



目录

  • 基于C#的图书借阅管理系统
  • 一、数据库设计
  • 数据库代码(SQL Sever)
  • 链接数据库
  • 二、Vs 2022后台代码
  • 1.登录界面代码
  • 2.图书信息界面代码
  • 3.图书编辑页面代码
  • 4.借阅记录界面代码


一、数据库设计

根据需求分析,一个基本的图书借阅管理系统数据库大致包括4个表:用户信息表(ReaderUser)、管理员信息表(AdminTable)、图书信息表(BookInfo)、借书表(Borrow)。分别存放相应子功能模块的数据信息,各表之间相互关联,数据统一操作。

数据库代码(SQL Sever)

create database BookManger
go 
use BookManger
go
--管理员表格
create table AdminTable
(
code nvarchar(50) not null,			--账号
name varchar(20) not null,			--姓名
pwd nvarchar(100) not null,			--密码
constraint pk_AdminTable primary key(code)
)
go
insert into AdminTable values('123456','张三','123456')
--用户表格

create table ReadUser
(
R_code nvarchar(50) not null,			--账号
R_pwd nvarchar(100) not null,			--密码
R_name nvarchar(20) not null,			--姓名
constraint pk_ReadUser primary key(R_code)
)
go
--图书信息
create table BookInfo
(
id int identity(1,1) not null,		--主键(自增长主键)
Bname nvarchar(100) not null,		--图书名称
ISBN nvarchar(50) not null,			--ISBN编码
publisher nvarchar(100) not null,	--出版社
publiDate datetime not null	,		--出版日期
sl int not null,					--图书数量
price money not null,				--单价
constraint pk_BookInfo primary key(id)
)
go
--借书表(主表)
create table Borrow
(
id int identity(1,1) not null,				--主键(自增长主键)
U_code nvarchar(50) not null,				--读者账号
U_name nvarchar(50) not null,
B_id int not null,							--读者Id
B_date datetime default getdate() not null,	--借书日期
B_day  int not null,						--借书天数
R_date datetime null,						--还书日期
B_state nvarchar(20) default N'借阅中' not null,				--借还书状态:借阅中、如期归还、超期归还、超期未还
constraint pk_Borrow primary key(id)
)
go

链接数据库

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;

public class DAB
{
    const string connectionString = @"server=127.0.0.1;database=BookManger;Uid=sa;pwd=123456;";//访问数据库
    public static SqlConnection Connection()
    {
        SqlConnection con = new SqlConnection();//创建连接
        con.ConnectionString = connectionString;//connectionString打开数据库的字符串
        return new SqlConnection();
    }
    //更新数据
    public static int ExecuteNonQuery(String sql)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = connectionString;
        con.Open();//打开并连接数据库
        //创建命令存储过程
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = System.Data.CommandType.Text;//设置如何解释CommandText 值(即设置CommandText所代表的含义)
        cmd.CommandText = sql;//执行的类型
        int ret = cmd.ExecuteNonQuery();
        con.Close();
        return ret; 
    }

    public static DataTable ExecuteDataTable(String sql)
    {
        SqlConnection con = new SqlConnection();
        con.ConnectionString = connectionString;
        con.Open();
        //创建命令
        SqlCommand cmd = con.CreateCommand();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.CommandText = sql;
        //填充数据
        DataTable dt = new DataTable();       
        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter();//数据的更新
        sqlDataAdapter.SelectCommand = cmd;
        sqlDataAdapter.SelectCommand.Connection = con;
        sqlDataAdapter.Fill(dt);//将数据放入表格
        con.Close();
        return dt;
    }
}

二、Vs 2022后台代码

1.登录界面代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string code = TextBox1.Text;
        string pwd = TextBox2.Text;
        string role = DropDownList1.Text;
        if (string.IsNullOrEmpty(code))//判断--是否为空
        {
            Response.Write("<script>alert('请输入账号');</script>");
            return;
        }
        if (string.IsNullOrEmpty(pwd))
        {
            Response.Write("<script>alert('请输入密码');</script>");
            return;
        }
        if (string.IsNullOrEmpty(role))
        {
            Response.Write("<script>alert('请选择用户');</script>");
            return;
        }
        if(role == "管理员")
        {
            string sql = "select * from AdminTable where code='" + code + "' and pwd='" + pwd + "'";
            DataTable table= DAB.ExecuteDataTable(sql);
            if(table.Rows.Count > 0)
            {
                //设置会话
                Session["Login_code"] = code;//存储特定的用户会话所需的信息
                Session["Login_name"] = table.Rows[0]["name"];
                Session["Login_role"] = role;
                Response.Write("<script>alert('管理员登记成功');window.location.href='BookManger/Default.aspx';</script>");
                Response.Redirect("Default.aspx");
                //return;
            }
            else
            {
                Response.Write("<script>alert('账号或密码有误');</script>");
                return;
            }
        }
        else
        {
            string sql = "select * from ReadUser where R_code='" + code + "' and R_pwd='" + pwd + "'";
            DataTable table = DAB.ExecuteDataTable(sql);
            if (table.Rows.Count > 0)
            {
                Session["Login_code"] = code;
                Session["Login_name"] = table.Rows[0]["R_name"];
                Session["Login_role"] = role;
                Response.Write("<script>alert('读者登录成功');window.location.href='BookManger/Default.aspx';</script>");
                Response.Redirect("DefaultUser.aspx");
            }
            else
            {
                Response.Write("<script>alert('输入密码有误');</script>");
                return;
            }
        }
    }
}

2.图书信息界面代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BookList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button1_Click(null, null);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string Bname = TextBox1.Text;
        string isbn = TextBox2.Text;
        string sql = "select * from BookInfo where 1=1";
        if (Bname.Length>0)
        {
            sql += " and Bname like N'%" + Bname + "%'";//模糊查询 %
        }
        if (isbn.Length > 0)
        {
            sql += " and ISBN = '" + isbn + "'";
        }
        //查询数据
        DataTable table = DAB.ExecuteDataTable(sql);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
}

3.图书编辑页面代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
//添加功能
public partial class EditBook : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string id = Request.QueryString["id"];
            string sql = "select * from BookInfo where id =" + id;
            DataTable table = DAB.ExecuteDataTable(sql);
            if (table.Rows.Count > 0)
            {
                TextBox1.Text = table.Rows[0]["Bname"].ToString();
                TextBox2.Text = table.Rows[0]["ISBN"].ToString();
                TextBox3.Text = table.Rows[0]["publisher"].ToString();
                TextBox4.Text = ((DateTime)table.Rows[0]["publiDate"]).ToString("yyyy-MM-dd");
                TextBox5.Text = table.Rows[0]["sl"].ToString();
                TextBox6.Text = table.Rows[0]["price"].ToString();
            }
            else
            {
                Response.Write("<script>alert('图书信息不存在!');window.location.href='BookInfo.aspx';</script>");//BookManger119HJX/
                return;
            }
        }
    }
    //添加图书按钮添加事件
    protected void Button1_Click(object sender, EventArgs e)
    {
        string Bname = TextBox1.Text;
        string isbn = TextBox2.Text;
        string publisher = TextBox3.Text;
        string publisDate = TextBox4.Text;
        string sl = TextBox5.Text;
        string price = TextBox6.Text;
        if (Bname.Length <= 0)
        {
            Response.Write("<script>alert('请输入图书名称');</script>");
            return;
        }
        if (isbn.Length <= 0)
        {
            Response.Write("<script>alert('请输入ISBN');</script>");
            return;
        }
        if (publisher.Length <= 0)
        {
            Response.Write("<script>alert('请输入出版社');</script>");
            return;
        }
        if (publisDate.Length <= 0)
        {
            Response.Write("<script>alert('请输入出版日期');</script>");
            return;
        }
        if (sl.Length <= 0)
        {
            Response.Write("<script>alert('请输入图书数量');</script>");
            return;
        }
        if (price.Length <= 0)
        {
            Response.Write("<script>alert('请输入图书单价');</script>");
            return;
        }

        string sql = "update BookInfo set Bname = '" + Bname + "',ISBN='" + isbn + "',publisher='" + publisher + "', publiDate='" + publisDate + "',sl='" + sl + "',price='" + price + "' where id =" + Request.QueryString["id"];
        DAB.ExecuteNonQuery(sql);

        Response.Write("<script>alert('修改成功');window.location.href='BookManger/BookInfo.aspx';</script>");
        Response.Redirect("BookInfo.aspx");//跳转到BookInfo界面。
        //return;
    }
}

4.借阅记录界面代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class BorrowRecord : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Button1_Click(null, null);
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string U_code = TextBox1.Text;
        string U_name = TextBox2.Text;
        string sql = "select BookInfo.*, Borrow.id as B_id,Borrow.B_date,Borrow.B_day,Borrow.R_date,Borrow.B_state ,Borrow.B_state,Borrow.U_code,ReadUser.R_name as U_name from BookInfo ,Borrow ,ReadUser where BookInfo.id=Borrow.B_id and borrow.U_code=ReadUser.R_code";
        if (U_name.Length>0)
        {
            sql += " and ReadUser.R_name like N'%" + U_name + "%'";
        }
        if (U_code.Length > 0)
        {
            sql += " and U_code = '" + U_code  + "'";
        }
        //查询数据
        DataTable table = DAB.ExecuteDataTable(sql);
        GridView1.DataSource = table;
        GridView1.DataBind();
    }
}