C#实现图书管理系统
时间:2022-12-23 11:50:03|栏目:.NET代码|点击: 次
本文为大家分享了C#实现图书管理系统课程设计,供大家参考,具体内容如下
一、设计目的
通过模拟图书管理系统,实现以下功能
学生账号的注册
学生对馆藏图书状况的查询
学生借书,还书状态的查询
学生借书以及归还图书
学生欠款的查询
二、框架分析
数据库主要包括:
学生表:学生学号,学生姓名,学生性别,学生类型,学生密码
图书表:图书编号,图书名,图书出版社,图书作者,图书状态(1代表可以借,0代表不可以,)图书类型,图书出版日期
借书表:学生号,图书号,图书名,借出日期
还书表:学生号,图书号,图书名,归还日期
欠费金额表:学生号,图书号,图书名,欠费金额(超过30天每天收0.3元)
三、实验环境
VS2013,SQLSERVER2014
四、体系结构
在客户端与数据库之间增加了一个“中间层”,分为表示层,业务逻辑层,数据访问层
表示层:为客户提供对应程序的访问
业务逻辑层:以类库的形式为表示层服务
数据访问层:实现整个系统的数据库连接,数据库存取操作,以组件类库的形式为业务逻辑层提供服务
五、代码实现
数据库的建立:
create database MyLibrary on (name=Mylibrary_data1, filename='d:\Mylibrary_data1.mdf', size=10, maxsize=50, filegrowth=4) log on (name=Mylibrary_log1, filename='d:\Mylibrary_log1.ldf', size=10MB, maxsize=20MB, filegrowth=2MB) use MyLibrary; create table reader( readerid varchar(20) primary key, readername varchar(20) not null, readersex varchar(20) not null, readertype varchar(20) not null, readerpd varchar(20) not null ) create table book( bookid varchar(20) primary key, booktype varchar(20) not null, bookauthor varchar(20) not null, bookname varchar(20) not null, isbrrowed varchar(20) not null, booklibrary varchar(20) not null, bookpubdate datetime ) create table brrow_data( readerid varchar(20) primary key, bookid varchar(20) , bookname varchar(20), brrowdate datetime foreign key (readerid) references reader(readerid), foreign key (bookid) references book(bookid) ) create table return_record( readerid varchar (20) primary key, bookid varchar(20), bookname varchar(20), returndate datetime, foreign key (readerid) references reader(readerid), foreign key (bookid) references book(bookid) ) create table readerfee( readerid varchar(20) primary key, bookid varchar(20), bookname varchar(20), readerpay varchar(20), foreign key (readerid) references reader(readerid), foreign key (bookid) references book(bookid) ) --已有图书插如表中 insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921000','缥缈之旅','修真','小猪','仙王出版社','2005-09-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921001','唐朝好男人','穿越','多一半','新星出版社','2005-07-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921002','鬼吹灯','恐怖','天下霸唱','安徽文艺出版社','2005-09-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921003','和空姐同居的日子','都市','海王','中国海关出版社','2005-08-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921004','卡图','科幻','方向','广西人民出版社','2005-06-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921005','蜀山剑侠传','仙侠','还珠楼主','陕西人民出版社','2005-04-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921006','何以笙箫默','言情','顾漫','朝华出版社','2005-05-01','否'); insert into book (bookid,bookname,booktype,bookauthor,booklibrary,bookpubdate,isbrrowed) values ('3140921007','步步惊心','穿越','童话','民族出版社','2005-12-01','否'); --将读者插如表中 insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007000','李磊','男','学生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007001','棠张僧','男','学生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007002','韩梅梅','女','学生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007003','路西','女','学生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007004','王强','男','学生','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007005','马正标','男','管理','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007006','卢海鹏','男','管理','000000'); insert into reader(readerid,readername,readersex,readertype,readerpd) values ('21007007','李艳玲','女','管理','000000'); --查询功能 select *from reader; select *from book; select *from brrow_data; select *from return_record; select *from readerfee; --删除表 drop table reader; drop table book; drop table brrow_data; drop table return_record; drop table readerfee;
登录界面的编辑
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace Library { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void panel1_Paint(object sender, PaintEventArgs e) { } //登录按钮 private void button1_Click(object sender, EventArgs e) { Form2 form2 = new Form2(); this.Hide(); form2.Show(); mycon.Close(); //sql = string.Format("select count(*) from reader where readerid='{0}'",textBox1.Text); //SqlCommand com = new SqlCommand(sql,mycon); //int count = (int)com.ExecuteScalar(); //if (count==1) //{ // sql = string.Format("select count(*) from reader where readerpd='{0}'",textBox2.Text); // com = new SqlCommand(sql, mycon); // count = (int)com.ExecuteScalar(); // if (count>=1) // { // if (comboBox1.Text=="学生") // { // Form2 form2 = new Form2(); // this.Hide(); // form2.Show(); form2.readerid = textBox1.Text; // mycon.Close(); // } // else if (comboBox1.Text=="管理员") // { // Form4 form4 = new Form4(); // this.Hide(); // form4.Show(); // mycon.Close(); // } // else // { // MessageBox.Show("请选择您的身份!"); // } // } // else // { // MessageBox.Show("密码错误,请重新输入!"); // } //} //else{ // MessageBox.Show("账号不存在,请重新输入!"); //} } string sql, con; SqlConnection mycon; //加载事件 private void Form1_Load(object sender, EventArgs e) { con = "Server=.;Database=MyLibrary;Trusted_Connection=SSPI"; mycon = new SqlConnection(con); mycon.Open(); } //注册 private void button2_Click(object sender, EventArgs e) { Form3 form3 = new Form3(); this.Hide(); form3.Show(); } } }
注册界面的编辑
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace Library { public partial class Form3 : Form { public Form3() { InitializeComponent(); } private void label3_Click(object sender, EventArgs e) { } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { } //提交申请 private void button1_Click(object sender, EventArgs e) { sql = string.Format("select count(*) from reader where readerpd='{0}'", textBox2.Text); SqlCommand com = new SqlCommand(sql, mycon); int count = (int)com.ExecuteScalar(); if (count==1) { MessageBox.Show("抱歉,该账号已存在!"); } else { if (textBox1.Text==""||textBox2.Text==""||textBox4.Text==""||comboBox1.Text==""||textBox3.Text=="") { MessageBox.Show("请将信息填写完整!"); } else { Form1 form1 = new Form1(); form1.Show(); this.Hide(); } } } string sql, con; SqlConnection mycon; private void Form3_Load(object sender, EventArgs e) { con = "Server=.;Database=MyLibrary;Trusted_Connection=SSPI"; mycon = new SqlConnection(con); mycon.Open(); } } }
学生管理界面的编辑
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; namespace Library { public partial class Form2 : Form { public Form2() { InitializeComponent(); } public string readerid; string sql, con; SqlConnection mycon; SqlCommand com; //加载事件 private void Form2_Load(object sender, EventArgs e) { label1.Hide(); textBox1.Hide(); button4.Hide(); dataGridView1.Hide(); con = "Server=.;Database=MyLibrary;Trusted_Connection=SSPI"; mycon = new SqlConnection(con); mycon.Open(); } //个人信息 private void button1_Click(object sender, EventArgs e) { dataGridView1.Show(); label1.Hide(); textBox1.Hide(); button4.Hide(); sql = string.Format("select * from reader where readerid='{0}'", readerid); SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "reader"); dataGridView1.DataSource = myds.Tables["reader"]; } //图书信息 private void button2_Click(object sender, EventArgs e) { dataGridView1.Show(); label1.Hide(); textBox1.Hide(); button4.Hide(); sql = "select * from book"; SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "book"); dataGridView1.DataSource = myds.Tables["book"]; } //缴费系统 private void button3_Click(object sender, EventArgs e) { try { sql = string.Format("insert into readerfee(readerid,bookid,bookname,readerpay)select readerid ,bookid , bookname,0.03*(datediff(day,convert(smalldatetime,brrowdate),getdate())-30) from brrow_data where datediff(day,convert(smalldatetime,brrowdate),getdate())>=0;"); com = new SqlCommand(); com.Connection = mycon; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr = com.ExecuteReader(); dr.Close(); } catch { } sql = string.Format("select * from readerfee where readerid='{0}'", readerid); SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "readerfee"); dataGridView1.DataSource = myds.Tables["readerfee"]; } //借还记录 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { dataGridView1.Show(); label1.Hide(); textBox1.Hide(); button4.Hide(); if (comboBox1.Text == "借书记录") { sql = string.Format("select * from brrow_data where readerid='{0}'", readerid); SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "reader"); dataGridView1.DataSource = myds.Tables["reader"]; } else { sql = string.Format("select * from return_record where readerid='{0}'", readerid); SqlDataAdapter mydata = new SqlDataAdapter(sql, con); DataSet myds = new DataSet(); mydata.Fill(myds, "reader"); dataGridView1.DataSource = myds.Tables["reader"]; } } private void comboBox2_SelectedIndexChanged(object sender, EventArgs e) { dataGridView1.Hide(); if (comboBox2.Text == "我要借书") { button4.Show(); button4.Text = "借阅本书"; label1.Text = "搜索书名"; label1.Show(); textBox1.Show(); } else if (comboBox2.Text == "我要还书") { button4.Show(); button4.Text = "归还本书"; label1.Text = "所借书号"; label1.Show(); textBox1.Show(); } } private void button4_Click(object sender, EventArgs e) { if (button4.Text == "借阅本书") { sql = string.Format("select count(*) from book where bookname='{0}'", textBox1.Text); com = new SqlCommand(sql, mycon); int count = (int)com.ExecuteScalar(); if (count >= 1) { sql = string.Format("select bookid from book where bookname='{0}'", textBox1.Text); com = new SqlCommand(sql, mycon); string bookid = (string)com.ExecuteScalar(); sql = string.Format("insert into brrow_data (readerid,bookid,bookname,brrowdate) values('{0}','{1}','{2}','{3}')", readerid, bookid, textBox1.Text, DateTime.Now); com = new SqlCommand(); com.Connection = mycon; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr = com.ExecuteReader(); dr.Close(); MessageBox.Show("借书成功!"); } else MessageBox.Show("抱歉,不存在本书!"); } else if (button4.Text == "归还本书") { sql = string.Format("select count(*) from brrow_data where bookid='{0}'", textBox1.Text); com = new SqlCommand(sql, mycon); int count = (int)com.ExecuteScalar(); if (count >= 1) { sql = string.Format("select bookname from brrow_data where bookid='{0}'", textBox1.Text); com = new SqlCommand(sql, mycon); string bookname = (string)com.ExecuteScalar(); sql = string.Format("insert into return_record(readerid,bookid,bookname,returndate) values ('{0}','{1}','{2}','{3}')", readerid, textBox1.Text, bookname, DateTime.Now); com = new SqlCommand(); com.Connection = mycon; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr = com.ExecuteReader(); dr.Close(); sql = string.Format("delete from brrow_data where bookid='{0}'", textBox1.Text); com = new SqlCommand(); com.Connection = mycon; com.CommandType = CommandType.Text; com.CommandText = sql; SqlDataReader dr1 = com.ExecuteReader(); dr1.Close(); MessageBox.Show(bookname + "还书成功!"); } else { MessageBox.Show("抱歉!您没有借这本书!"); } } } } }
六,心得总结
通过对一系列功能的实现,了解到了如何如何建立数据库并进行增删改查等基本操作,如何实现数据库与winform应用程序之间的连接,如何通过C#语言来操控数据库。更重要的是意识到了三层架构的重要性,将会加强学习这一方面。