C#小型书店管理系统

一次非常简单的课程设计,资源链接如下:

课程设计源代码、论文:https://download.csdn.net/download/QQwli/14801274

一、开发技术和环境

(1)开发环境
开发系统:Windows 10操作系统;
开发工具:Visual Studio 2010
开发语言:C#
(2)数据库
SQL Server (SQLEXPRESS)
(3)知识点
本项目应用了如下知识点:
C#语言基础、选择结构、循环结构
面向对象、多态
窗体、通用对话框
数据库

二、系统设计

在这里插入图片描述

三、系统实现

1.注册登录

该界面主要实现登录注册功能,点击注册会自动把用户信息添加到数据库内,保存管理员信息;登录功能实现登录到管理界面,输入密码会自动隐藏密码内容,并有复选框实现显示密码功能。实现代码如下:
在这里插入图片描述
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lesson_designer
{
    public partial class Form_登录 : Form
    {
        public Form_登录()
        {
            InitializeComponent();
        }
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataReader reader = null;
        private string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#实验报告\lesson_designer\lesson_designer\DB_username.mdf;Integrated Security=True;User Instance=True";

        private void btn_register_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "INSERT INTO username_Table (username, password) VALUES  (@username, @password)";
                cmd.Parameters.AddWithValue("@username",textBox1.Text.Trim());
                cmd.Parameters.AddWithValue("@password",textBox2.Text.Trim());
                if (String.IsNullOrEmpty(textBox1.Text))
                {
                    MessageBox.Show("用户名不能为空!");
                    return;
                }
                if (String.IsNullOrEmpty(textBox2.Text))
                {
                    MessageBox.Show("密码不能为空!");
                    return;
                }
                int n = cmd.ExecuteNonQuery();
                if (n>0)
                {
                    MessageBox.Show("成功注册,请登录","insert ok",MessageBoxButtons.OK,MessageBoxIcon.Information);
                }               
                else
                {
                    MessageBox.Show("注册失败", "insert error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message,"open error",MessageBoxButtons.OK);

            }
            finally
            {
                if (conn.State==ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        private void btn_login_Click(object sender, EventArgs e)
        {
            try
            {
                
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT  username_Table.* FROM   username_Table where(username=@username) and (password=@password)";
                cmd.Parameters.AddWithValue("@username",textBox1.Text.Trim());
                cmd.Parameters.AddWithValue("@password",textBox2.Text.Trim());
                reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    MessageBox.Show("登陆成功", "登陆成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    Form_管理 mag = new Form_管理();
                    mag.ShowDialog();
                    this.Close();
                }
                else
                {
                    MessageBox.Show("用户名、密码不正确", "登陆失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "open error", MessageBoxButtons.OK);
            }
            finally
            {
                if (reader!=null)
                {
                    reader.Close();
                }
                if (conn.State==ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        private void checkBox1_CheckedChanged_1(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox2.PasswordChar = '\0';
            }
            else
            {
                textBox2.PasswordChar = '*';
            }
        }
    }
}

2.管理

登录成功后进入管理界面,可在该界面选择相应的功能,包括:查询、录入、删除、销售、管理员的显示以及退出。
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace lesson_designer
{
    public partial class Form_管理 : Form
    {
        public Form_管理()
        {
            InitializeComponent();
        }

        private void btn_seach_Click(object sender, EventArgs e)
        {
            Form_查询 sch = new Form_查询();
            sch.Show();
        }

        private void btn_enter_Click(object sender, EventArgs e)
        {
            Form_录入 enter = new Form_录入();
            enter.Show();
        }

        private void btn_sell_Click(object sender, EventArgs e)
        {
            Form_销售 sell = new Form_销售();
            sell.Show();
        }

        private void btn_quit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_show_Click(object sender, EventArgs e)
        {
            Form_管理员显示 dis = new Form_管理员显示();
            dis.Show();
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            Form_删除 del = new Form_删除();
            del.Show();
        }
    }
}

3.查询

查询界面实现输入图书的名称,显示该图书的所有信息,实现代码如下:
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lesson_designer
{
    public partial class Form_查询 : Form
    {
        public Form_查询()
        {
            InitializeComponent();
        }
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataAdapter adapter = null;
        private DataSet ds = null;
        private string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#实验报告\lesson_designer\lesson_designer\DB_Book.mdf;Integrated Security=True;User Instance=True";
        private void btn_seach_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT  id, name, pub, author, price, num FROM      Book_Table WHERE   (name = "+ "'"+textBox2.Text.ToString()+"'" +")";
                ds = new DataSet();
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds, "books");
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = "books";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "seach Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { 
                 if (conn.State == ConnectionState.Open)
                     conn.Close();
            }
        }
    }
}

4.录入修改

该界面主要实现图书的录入、修改以及刷新显示所有录入的图书,输入图书的id、书名、数量、作者、出版社、单价后即可实现录入功能,修改功能通过图书的id号、实现对图书各属性的修改。
刷新
录入
修改

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lesson_designer
{
    public partial class Form_录入 : Form
    {
        public Form_录入()
        {
            InitializeComponent();
        }
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataAdapter adapter = null;
        private DataSet ds = null;
        private string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#实验报告\lesson_designer\lesson_designer\DB_Book.mdf;Integrated Security=True;User Instance=True";
        private void btn_enter_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "insert into Book_Table ( id, name, pub, author, price, num) values(@id,@name,@pub,@author,@price,@num)";
                cmd.Parameters.AddWithValue("@id",tb_id.Text.Trim());
                cmd.Parameters.AddWithValue("@name", tb_name.Text.Trim());
                cmd.Parameters.AddWithValue("@pub", tb_pub.Text.Trim());
                cmd.Parameters.AddWithValue("@author", tb_auth.Text.Trim());
                cmd.Parameters.AddWithValue("@price", tb_price.Text.Trim());
                cmd.Parameters.AddWithValue("@num", tb_num.Text.Trim());

                int n = cmd.ExecuteNonQuery();
                if (n > 0)
                {
                    MessageBox.Show("成功录入", "insert ok", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("录入失败", "insert error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "insert error", MessageBoxButtons.OK);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
        private void btn_re_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT  Book_Table.* FROM      Book_Table";
                ds = new DataSet();
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(ds, "books");
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = "books";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "seach Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
        }
        private void btn_update_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "UPDATE  Book_Table SET   name = " + "'" + tb_name.Text.ToString() + "'" + " ,pub= " + "'" + tb_pub.Text.ToString() + "'" +",author="+"'"+tb_auth.Text+"'"+",price="+"'"+tb_price.Text+"'"+",num="+"'"+tb_num.Text+"'"+" "+"WHERE   (id =  " + "'" + tb_id.Text.ToString() + "'" + " )";
                MessageBox.Show("成功修改", "update ok", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "update  error", MessageBoxButtons.OK);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
    }
}

5.删除

删除界面通过输入图书的id号,实现对所有图书信息的修改。
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lesson_designer
{
    public partial class Form_删除 : Form
    {
        public Form_删除()
        {
            InitializeComponent();
        }
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB_Book.mdf;Integrated Security=True;User Instance=True";
        private void btn_del_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "DELETE FROM Book_Table WHERE   (name ="+"'"+tb_id.ToString()+"'"+")";
                MessageBox.Show("成功删除", "del ok", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "del error", MessageBoxButtons.OK);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
        }
    }
}

6.销售

该界面实现对图书的销售,输入图书的id号以及购买图书的数量后会自动计算出所需价格,并自动更新数据库该图书的库存量。
在这里插入图片描述
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lesson_designer
{
    public partial class Form_销售 : Form
    {
        public Form_销售()
        {
            InitializeComponent();
        }
        private static double num = 0;
        private static double num1 = 0;
        private SqlConnection conn = null;
        private SqlCommand cmd= null;
        private SqlDataReader reader = null;
        private string connstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#实验报告\lesson_designer\lesson_designer\DB_Book.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                double price=0;
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT  price FROM      Book_Table WHERE   (id = " + "'" + tb_id.Text.ToString() + "'" + ")";
                reader = cmd.ExecuteReader();
                if (reader!=null)
                {
                    if (reader.Read())
                    {
                        price=0;
                       price= double.Parse(reader.GetString(0));
                    }
                }
                num = double.Parse(tb_num.Text);
                tb_price.Text = (num * price).ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "select price Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            try
            {
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT  num FROM      Book_Table WHERE   (id = " + "'" + tb_id.Text.ToString() + "'" + ")";
                double num1 = 0;
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    if (reader.Read())
                    {
                        num1 = int.Parse(reader.GetString(0));
                    }
                }
                num1 = num1 - num;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "select num error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
            try
            {
                conn = new SqlConnection(connstring);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "UPDATE  Book_Table SET   num =" + "'" + num1.ToString() + "'" +" "+"WHERE   (id = " + "'" + tb_id.Text.ToString() + "'" + ")";
                MessageBox.Show("更新成功","ok", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "update num eorror", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                    conn.Close();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

7.管理员显示

该界面实现对所有管理员信息的显示,显示出用户名以及相应的密码。实现代码如下:
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace lesson_designer
{
    public partial class Form_管理员显示 : Form
    {
        public Form_管理员显示()
        {
            InitializeComponent();
        }
        private SqlConnection conn = null;
        private SqlCommand cmd = null;
        private SqlDataAdapter adapter = null;
        private DataSet dset = null;
        private string stringconn = @"Data Source=.\SQLEXPRESS;AttachDbFilename=E:\C#实验报告\lesson_designer\lesson_designer\DB_username.mdf;Integrated Security=True;User Instance=True";
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                conn = new SqlConnection(stringconn);
                conn.Open();
                cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT  username_Table.* FROM      username_Table";
                dset = new DataSet();
                adapter = new SqlDataAdapter(cmd);
                adapter.Fill(dset,"user");               
                dataGridView1.DataSource = dset;
                dataGridView1.DataMember = "user";
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message, "eorror", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (conn.State == ConnectionState.Open) conn.Close();
            }
        }
    }
}

四、总结

本系统设计初衷是为了满足目前图书零售行业发展的需要,专门用来管理书店图书、员工、库存销售的软件,中小型书店企业将是本软件的极大受益者。经过测试,本系统稳定性还需改善,所有的需求功能都能够实现,本系统采用的时 Microsoft 旗下的 SQL Server 数据库,拥有 Microsoft 强大的技术支持,数据安全性能够得到很好的保障,解决了书店对公司数据信息安全的担忧。本系统是在 windows 系统下运行。系统按照功能分模块设计,操作比较简洁易懂,员工可以很容易上手。与传统管理模式相比较,本系统的使用不仅可以节省大量的人力物力,降低公司的经营成本,而且可以极大的减少差错,提高公司的工作效率。公司只有工作中减少了差错,服务质量才会上升,才能够赢得顾客的满意度,从而加大公司的客流量。

  • 9
    点赞
  • 130
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值