图书管理系统课程设计报告

《管理信息系统实习》报告

专 业 班 级                          

学 生 姓 名                           

指 导 教 师        王 桃 群           

时       间     2012.3.13~2012.3.23 

成       绩                           

评       语                            
一、课程设计题目

       图书管理系统

二、系统需求

1.系统的准备

操作系统:Windows xp

数据库系统:SQL Server 20## 或 SQL Server 2005

客户端开发工具:Visual Studio 2005或其他开发工具

2.知识准备

熟悉SQL Server 20## 或 SQL Server 2005的使用;

熟悉C#、ASP.NET或其他语言进行数据库编程。

3.系统分析

图书信息包括:每种图书都有书名、ISBN、一名或多名作者(译者)、出版社、定价和内容简介等;

读者信息包括:借书证记录有借阅者的姓名、密码、所在单位和类别等;

读者凭借书证借书,教师最多借书15本书,借书期限最长为90天,学生最多借书8本书,借书期限最长为30天。对于超期未还的读者不能继续借书,每本书每超期一天罚款0.05元。

三、系统设计

1.体系结构

本系统使用c/s模式的两层结构,表示层(USL)和数据访问层(DAL)。

表示层(USL):为客户提供对应用程序的访问,以Windows应用程序或Web应用程序的形式提供实现的功能。

业务逻辑层(BLL):实现应用程序的业务功能,以类库的形式为表示层提供服务。

数据访问层(DAL):实现整个系统所有的数据库连接、数据存取操作,以组件类库的形式为业务逻辑层提供服务。

此外,实体类,简单地说是描述一个业务实体的类。业务实体直观一点的理解就是整个应用系统业务所涉及的对象,从数据存储来讲,业务实体就是存储应用系统信息的数据表,将数据表中的每一个字段定义成属性,并将这些属性用一个类封装,这个类就称为实体类。

2.功能模块框图

 

3.数据库设计

1. 读者类别表(ReaderType)

2. 读者信息表(Reader)

3. 图书信息表(Book)

4. 借阅信息表(Borrow)

四、系统实现

  登录的代码实现:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace BooksMng

{

    public partial class BookLogin : Form

    {

        public BookLogin()

        {

            InitializeComponent();

        }

        private void textBox2_TextChanged(object sender, EventArgs e)

        {

        }

        private void btnlogin_Click(object sender, EventArgs e)

        {

            //连接数据库

            SqlConnection conn = new SqlConnection("server=.;database=Booksmng; integrated security=True");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            //cmd.CommandText="select count(*) from Users where userName='"+txtName.Text+"'and userPwd='"+txtPwd.Text+"'";

            cmd.CommandText = "select count(*) from Users where userName=@userName and userPwd=@userPwd";

            cmd.Parameters.Add("@userName", SqlDbType.VarChar, 20).Value = txtName.Text;

            cmd.Parameters.Add("@userPwd", SqlDbType.VarChar, 20).Value = txtPwd.Text;

            try

            {

                int count = Convert.ToInt32(cmd.ExecuteScalar());

                if (count != 0)

                {

                    MessageBox.Show("登陆成功!");

                    BookMain frm = new BookMain();

                    frm.Show();

                }

            }

            catch (SqlException ex)

            {

                //MessageBox.Show("登录失败!"); 

                MessageBox.Show(ex.Message);

            }

        }

        private void FrmLogin_Load(object sender, EventArgs e)

        {

        }

        }

    

        }

图书管理部分,主要的代码实现如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace BooksMng

{

    public partial class BookManage : Form

    {

        public BookManage()

        {

            InitializeComponent();

        }

       private void Form2_Load(object sender, EventArgs e)

        {

            DataBind();

        }

        private void DataBind()

        {

            //连接数据库

            SqlConnection conn = new SqlConnection("server=.; database=BooksMng;integrated security=True");

            //SqlConnection conn = new SqlConnection("server=.; database=BooksMng;integrated security=True");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            //cmd.commandText = "select * from Book";

            cmd.CommandText = "select bkID 编号, bkName 书名,bkAuthor 作者,bkPages 页数,bkPress 出版社 from Book";

            SqlDataAdapter sda = new SqlDataAdapter(cmd);

            DataSet ds = new DataSet();

            sda.Fill(ds);

            dgvBooks.DataSource = ds.Tables[0];

            txtName.DataBindings.Clear();

            txtAuthor.DataBindings.Clear();

            txtPage.DataBindings.Clear();

            txtPress.DataBindings.Clear();

            txtName.DataBindings.Add("Text",ds.Tables[0],"书名");

            txtAuthor.DataBindings.Add("Text",ds.Tables[0],"作者");

            txtPage.DataBindings.Add("Text",ds.Tables[0],"页数");

            txtPress.DataBindings.Add("Text", ds.Tables[0], "出版社");

            //上面的代码是在窗体Load时,将Books表中的所有记录,即所有的图书信息显示在网格DataGrid空间中。

            //

        }

        //下面是实现添加功能

        private void btnAdd_click(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection("server=.; database=BooksMng;integrated security=True");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "insert into Book(bkName, bkAuthor,bkPages,bkPress) values(@bkName,@bkAuthor,@bkPages,@bkPress)";

            cmd.Parameters.Add("@bkName", SqlDbType.VarChar, 30).Value = txtName.Text;

            cmd.Parameters.Add("@bkAuthor", SqlDbType.VarChar, 30).Value = txtAuthor.Text;

            cmd.Parameters.Add("@bkPages", SqlDbType.Int).Value =Convert.ToInt32(txtPage.Text);//类型转换

            cmd.Parameters.Add("@bkPress", SqlDbType.VarChar, 50).Value = txtPress.Text;

            try

            {

                cmd.ExecuteNonQuery();           //执行上述SQL命令

                MessageBox.Show("图书添加成功!");

                DataBind();

                //重新将数据库绑定到DataGrid

            }

            catch (SqlException ex)

            {

                MessageBox.Show("图书添加失败");

                MessageBox.Show(ex.Message);

            }

        }

        private void btnSearch_Click(object sender, EventArgs e)

        {

            //连接数据库

            SqlConnection conn = new SqlConnection("server=.; database=BooksMng;integrated security=True");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            String sql = "";

            //按作者查找

            if (txtAuthor.Text != "")

            {

                sql += "select bkID 编号, bkName 书名,bkPages 页数,bkPress 出版社 from Book where bkAuthor=@bkAuthor";

            }

            try

            {

                cmd.CommandText=sql;

                cmd.Parameters.Add("@bkAuthor", SqlDbType.VarChar, 30).Value = txtAuthor.Text;

                SqlDataAdapter sda=new SqlDataAdapter(cmd);

                DataSet ds=new DataSet();

                sda.Fill(ds);

                dgvBooks.DataSource = ds.Tables[0];

            }

            catch(SqlException ex)

            {

                MessageBox.Show("查找失败");

                MessageBox.Show(ex.Message);

            }

        }

        private void btnDelete_Click(object sender, EventArgs e)

        {

            //连接数据库

            SqlConnection conn = new SqlConnection("server=.; database=BooksMng;integrated security=True");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "delete from Book where bkID=@bkID";

            cmd.Parameters.Add("@bkID", SqlDbType.Int).Value = Convert.ToInt32(dgvBooks[0, dgvBooks.CurrentRow.Index].Value);

            try

            {

                if (MessageBox.Show("确定要删除该图书吗?", "确定删除", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)

                {

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("删除成功!");

                    DataBind();

                }

            }

            catch (SqlException ex)

            {

                MessageBox.Show("删除失败");

                MessageBox.Show(ex.Message);

            }

        }

        //下面做更新图书信息

        private void btnUpdate_Click(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection("server=.; database=BooksMng;integrated security=True");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "update Book set bkName=@bkName, bkAuthor=@bkAuthor, bkPages=@bkPages,bkPress=@bkPress where bkID=@bkID";

            cmd.Parameters.Add("@bkID", SqlDbType.Int).Value = Convert.ToInt32(dgvBooks[0, dgvBooks.CurrentRow.Index].Value);

            cmd.Parameters.Add("@bkName", SqlDbType.VarChar, 30).Value = txtName.Text;

            cmd.Parameters.Add("@bkAuthor", SqlDbType.VarChar, 30).Value =txtAuthor.Text;

            cmd.Parameters.Add("@bkPages", SqlDbType.Int).Value = Convert.ToInt32(txtPage.Text);//类型转换

            cmd.Parameters.Add("@bkPress", SqlDbType.VarChar, 50).Value = txtPress.Text;

           

            try

            {

                if (MessageBox.Show("确定要更新图书信息吗?", "确认更新", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)

                {

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("更新成功!");

                    DataBind();

                }

            }

            catch (SqlException ex)

            {

                MessageBox.Show("更新失败");

                MessageBox.Show(ex.Message);

            }

        }

     

        }

       

        //图书可以添加成功

借书实现主要代码如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace BooksMng

{

    public partial class BookBorrow : Form

    {

        public BookBorrow()

        {

            InitializeComponent();

        }

        //获取读者可借天数

        private int GetLendDay(int rdID)

        {

            SqlConnection conn = new SqlConnection("server=.;database=BooksMng;integrated security=true");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "select CanLendDay from ReaderType where rdType=(select rdType from Reader where rdID=" + rdID + ")";

            return Convert.ToInt32(cmd.ExecuteScalar());

        }

        private void btnBorrow_Click(object sender, EventArgs e)

        {

            SqlConnection conn = new SqlConnection("server=.;database=BooksMng;integrated security=true");

            conn.Open();

            SqlCommand cmd = conn.CreateCommand();

            cmd.CommandText = "insert into Borrow(rdID,bkID,ldContinueTimes,IdDateOut,ldDateRetPlan,lsHasReturn) values(@rdID,@bkID,0,@IdDateOut,@ldDateRetPlan,0)";

            cmd.Parameters.Add("@rdID", SqlDbType.Int).Value = Convert.ToInt32(txtrdID.Text);

            cmd.Parameters.Add("@bkID", SqlDbType.Int).Value = Convert.ToInt32(txtbkID.Text);//类型转换

            cmd.Parameters.Add("@IdDateOut", SqlDbType.DateTime).Value = DateTime.Now;//借书时间为当前的系统时间

            //应还日期为=借书日期+可借天数

cmd.Parameters.Add("@ldDateRetPlan", SqlDbType.DateTime).Value = DateTime.Now.AddDays(GetLendDay(Convert.ToInt32(txtrdID.Text)));

            try

            {

                cmd.ExecuteNonQuery();

                MessageBox.Show("借书成功!");

            }

            catch (SqlException ex)

            {

                MessageBox.Show("借书失败");

                MessageBox.Show(ex.Message);

            }

        }

    }

}

五、系统运行效果

  图书管理模块的运行结果如下:

首先,设计一个用户登录界面,以管理员的身份登录来实现图书的添加、查找、删除、更新的功能。

  登录界面的设计:

登录成功的效果

登录成功以后,跳到图书管理主界面:图书管理主界面如下:

登录成功以后,跳到图书管理的页面:图书管理的页面如下:

 此界面可以对图书实现添加、查找、删除和信息更新这四个功能。

从图书管理主界面跳到借书界面:

六、遇到的问题及解决方法

      在实现借书这个功能时,老是借书失败,并且提示:

                                

凭借这个提示是不可能找到错误的,为了找到这个错误,我设置了一个断点如图:

                    

然后逐句运行,发现错误在 “catch (SqlException ex)”这句,并且提示:

LD}EN~I9UGAXJ{ZJF(W]O09

点击获取错误的帮助,软件给出的帮助是:

F)NMDI9R3]X`S@C@3KJV4QO

我不知道这是什么错误,但是我知道错误不在“catch (SqlException ex)”这句,因为每当执行“cmd.ExecuteNonQuery();”这句时,就会跳到“catch (SqlException ex)”这句,并且提示有错误。我上网搜索了“cmd.ExecuteNonQuery();”此语句,发现有这么一段解释:

     cmd.Parameters.Add(New               OleDbParameter("@用户名",               OleDbType.VarChar))
首先你的insertinto这个SQL语句是错误的,应该是insert   into   Enternumber(username,password,sex,work,tel,mail)               values   (@用户名,@密码,@性别,@职业,@电话,@邮箱)
其次就是最关键的错误,parameters这个方法使用时一定要有这样一个语句cmd.commandtype=CommandType.StoredProcedure这个语句的作用是用存储方法来传值的,也就是说在你的数据库中一定要一个存储过程,
parameters方法的使用前面应该有,cmd.commandtype=commandtype.storedprocedure   
                                                                  cmd.commandtext="存储过程"
                                                          cmd.parameters.add(new   oledbparameter("@存储变量的一个变量名",数值))

     此时我终于知道了,是数据库插入于具有问题,经过我反复的比较数据库中的各个键的属性,左最终改掉了所有的错误,程序运行正确。

七、心得与体会

这次的课程设计主要使用c#和SQL Server这两种知识来设计一个图书管理系统,而对于这两种知识我们曾经都开了课程,并且进行了系统的学习。我曾经自认为SQL Server还学得不错,因为书上的东西差不多都弄懂了,然而这次课程设计我却发现书上的东西我几乎忘得差不多了,一个很简单的查询语句都不知道怎么去写。我恍然间发觉自己做的很差,对于已经学的东西没有很好地进行运用,以至于很多已经学了的东西都已经忘记了。虽然如此,但老师仍很耐心的给我们讲解,知道我们如何一步一步地去做,真的很感谢老师为我们的付出。

通过这两个星期的课程设计,是我对所学知识有了更深一步的理解与掌握,理论与实践也能更好地结合在一起,这一过程中我遇到了很多困难,但这更使我觉得其中的乐趣和那种战胜困难后的成就感。同时感到学无止境,在今后的学习和工作中,我会不断地充实自己。

相关推荐