自己用C#做了一个简单的图书管理系统,发博客记录一下好心情,课程设计作业。
(1)创建数据库
利用SQL Sever2008创建一个MBOOK的数据库
(2)创建了四张基础表:TReader(读者信息表),TBook(图书信息表),TLend(借阅表),TUser(用户数据表)
(3)借书的存储过程
CREATE PROCEDURE Book_Borrow @in_ReaderID char(8), @in_ISBN char(18), @in_BookID char(10),
@out_str char(30) OUTPUT
AS
BEGIN
IF NOT EXISTS(SELECT * FROM TReader WHERE ReaderID=@in_ReaderID)
BEGIN
SET @out_str= ‘该读者不存在’
RETURN 0
END
IF NOT EXISTS(SELECT * FROM TBook WHERE ISBN=@in_ISBN)
BEGIN
SET @out_str= ‘该图书不存在’
RETURN 0
END
IF (SELECT Num FROM TReader WHERE ReaderID=@in_ReaderID)=5
BEGIN
SET @out_str= ‘读者借书量不能大于5’
RETURN 0
END
IF (SELECT SNum FROM TBook WHERE ISBN=@in_ISBN)=0
BEGIN
SET @out_str= ‘图书库存量为0’
RETURN 0
END
IF @in_ISBN IN (SELECT ISBN FROM TLend WHERE ReaderID=@in_ReaderID)
BEGIN
SET @out_str= ‘读者已经借过该书’
RETURN 0
END
IF EXISTS(SELECT * FROM TLend WHERE BookID=@in_BookID AND ReaderID=@in_ReaderID)
BEGIN
SET @out_str= ‘该图书ID已存在’
RETURN 0
END
BEGIN TRAN /开始一个事务/
INSERT INTO TLend VALUES(@in_BookID, @in_ReaderID, @in_ISBN, GETDATE())
IF @@ERROR>0 /如果前面一条SQL语句出错则回滚事务并返回/
BEGIN
ROLLBACK TRAN
SET @out_str= ‘执行过程中遇到错误’
RETURN 0
END
UPDATE TReader SET Num=Num+1 WHERE ReaderID=@in_ReaderID
IF @@ERROR>0 /如果前面一条SQL语句出错则回滚事务并返回/
BEGIN
ROLLBACK TRAN
SET @out_str= ‘执行过程中遇到错误’
RETURN 0
END
UPDATE TBook SET SNum=SNum1 WHERE ISBN=@in_ISBN
IF @@ERROR=0 /如果所有语句都不出错则结束事务并返回/
BEGIN
COMMIT TRAN
SET @out_str= ‘借书成功’
RETURN 1
END
ELSE /如果执行出错则回滚所有操作并返回/
BEGIN
ROLLBACK TRAN
SET @out_str= ‘执行过程中遇到错误’
RETURN 0
END
END
(4)还书的时候的触发器
USE MBOOK
GO
CREATE TRIGGER TLend_delete ON TLend
AFTER DELETE
AS
BEGIN
UPDATE TReader SET Num=Num-1 WHERE ReaderID=(SELECT ReaderID FROM deleted)
UPDATE TBook SET SNum=SNum+1 WHERE ISBN=(SELECT ISBN FROM deleted)
END
(5)还书时候的存储过程
USE [MBook]
GO
/****** Object: StoredProcedure [dbo].[Book_Return] Script Date: 06/03/2020 16:59:15 *****/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[Book_Return](@in_ReaderID nchar(10),@in_BookID nchar(10),@out_str char(30) OUTPUT)
AS
BEGIN
IF NOT EXISTS(SELECT * FROM TReader WHERE ReaderID=@in_ReaderID)
BEGIN
SET @out_str= ‘该读者不存在’
RETURN 0
END
IF NOT EXISTS(SELECTFROM TLend WHERE ReaderID=@in_ReaderID AND BookID=@in_BookID)
BEGIN
SET @out_str=‘该读者并未借过此书’
RETURN 0
END
BEGIN TRAN
DELETE FROM TLend WHERE ReaderID=@in_ReaderID AND BookID=@in_BookID
IF @@ERROR>0 /*如果前面一条SQL语句出错则回滚事务并返回*/
BEGIN
ROLLBACK TRAN
SET @out_str= '执行过程中遇到错误'
RETURN 0
END
IF @@ERROR=0
BEGIN
COMMIT TRAN
SET @out_str= '还书成功'
RETURN 1
END
ELSE /*如果执行出错则回滚所有操作并返回*/
BEGIN
ROLLBACK TRAN
SET @out_str= '执行过程中遇到错误'
RETURN 0
END
END
登录窗体
注意事项:匹配用户账号,用户密码,用于实现登录,忘记密码的时候可以进行找回密码,可以修改密码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BOOKSY
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
this.BackgroundImageLayout = ImageLayout.Stretch;
}
private void toolStripStatusLabel1_Click(object sender, EventArgs e)//忘记密码
{
}
private void button1_Click(object sender, EventArgs e)//登录
{
string id = textBox1.Text.Trim().ToString();
string pwd = textBox2.Text.Trim().ToString();
if(textBox1 .Text .Trim ()==""||textBox2 .Text .Trim() == "")
{
MessageBox.Show("请输入密码和账号!");
return;
}
else
{
string strcon = @"Data Source=(local);Initial Catalog=MBook;Persist Security Info=True;User ID=sa;Password='zdllxf211215'";
SqlConnection conn = new SqlConnection(strcon);
string strselect = "select Name from TUser where id='" + id + "' and pwd='" + pwd + "'";
SqlCommand cmd = new SqlCommand(strselect, conn);
conn.Open();
string name1 = cmd.ExecuteScalar().ToString();
if(name1 != "")
{
this.Hide();
BooKManage bookmanage = new BOOKSY.BooKManage();
bookmanage.name = name1;
bookmanage.ShowDialog();
}
else
{
MessageBox.Show("登录失败,密码或账号错误!");
}
}
}
private void button2_Click(object sender, EventArgs e)//取消
{
this.Close();
}
}
}
主界面窗体
1.菜单栏以及工具栏的使用,作为父窗体,,IsMdiContainer属性值设置为“True”,Text属性值设置为“图书管理系统”,Icon属性选择准备好的图标。BackgroundImage属性选择准备好的图片。
2.,分别双击“借书‘,“还书”、“读者管理”和 “图书管理”为其添加事件
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;
namespace BOOKSY
{
public partial class BooKManage : Form
{
public BooKManage()
{
InitializeComponent();
this.BackgroundImageLayout = ImageLayout.Stretch;//设置背景图片自动适应
}
private string _name;
public string name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private void 借书BToolStripMenuItem_Click(object sender, EventArgs e)//借书窗体
{
BorrowBook form2 = new BorrowBook();
form2.MdiParent = this;
form2.Show();
}
private void 还书RToolStripMenuItem_Click(object sender, EventArgs e)//还书窗体
{
ReturnBook form3 = new ReturnBook();
form3.MdiParent = this;
form3.Show();
}
private void 读者管理PToolStripMenuItem_Click(object sender, EventArgs e)//读者管理窗体
{
ReaderInfo form4 = new ReaderInfo();
form4.MdiParent = this;
form4.Show();
}
private void 图书管理MToolStripMenuItem_Click(object sender, EventArgs e)//图书管理窗体
{
BookInfo form5 = new BookInfo();
form5.MdiParent = this;
form5.Show();
}
private void BooKManage_Load(object sender, EventArgs e)//主页显示登录状态
{
tssl1.Text = name+"同学,你好,欢迎你使用图书管理系统";
}
private void timer1_Tick(object sender, EventArgs e)//主页显示时间
{
tssl2.Text = DateTime.Now.ToString();
}
}
}
借书窗体
1.点击查询按钮,利用借书证号进行匹配数据库,查询借阅表,并将借阅信息展现在下方DataGridView控件中
2.利用借书证号,图书ID,ISBN,利用ADO.NET技术操作数据库MBOOK,调用存储过程borrow_book,点击借书按钮,完成借书过程,并将更新后的借阅信息表更新在下方DataGridView控件中
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 BOOKSY
{
public partial class BorrowBook : Form
{
public BorrowBook()
{
InitializeComponent();
this.BackgroundImageLayout = ImageLayout.Stretch;//设置背景图片自动适应
dataGridView1.Columns[0].Width = 180;
}
string strcon = @"Data Source=(local);Initial Catalog=MBook;Persist Security Info=True;User ID=sa;Password='zdllxf211215'";
private void button1_Click(object sender, EventArgs e)//查询书籍
{
SqlConnection conn = new SqlConnection(strcon);//实例化连接对象
string sqlstrselect = "select[BookID],[ISBN],[BookName],[Publisher],[Price],[LTime]from [RBL] where [ReaderID]='" + textBox1.Text.Trim() + "'";//操作命令
try
{
SqlDataAdapter adapter = new SqlDataAdapter(sqlstrselect, conn);//创建新的适配器
DataSet distable = new DataSet();
adapter.Fill(distable, "testTable");
dataGridView1.DataSource = distable.Tables["testTable"];//将数据显示在dataGridView中
dataGridView1.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
conn.Close();//关闭连接
}
}
private void button2_Click(object sender, EventArgs e)//借书
{
if (textBox1.Text.Trim() == "" || textBox2.Text