首页 > 数据库 >C#连接数据库实现开发图书管理系统操作代码

C#连接数据库实现开发图书管理系统操作代码

时间:2023-01-05 11:35:37浏览次数:56  
标签:管理系统 C# Text 数据库 dao System dc using 图书

 

//客户端登录界面(Form1.cs窗口体系)

 

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 static System.Windows.Forms.VisualStyles.VisualStyleElement;

 

using System.Data.SqlClient;

 

 

 

namespace 图书管理系统1

 

{

 

    public partial class Form1 : Form

 

    {

 

        public Form1()

 

        {

 

            InitializeComponent();

 

        }

 

        private void button1_Click_1(object sender, EventArgs e)

 

        {

 

            if (textBox1.Text != "" && textBox2.Text != "")

 

            {

 

                Login();

 

            }

 

            else

 

            {

 

                MessageBox.Show("输入有空项,请重新输入!");

 

            }

 

        }

 

        //登陆方法,验证是否允许登录,允许返回真

 

        public void Login()

 

        {

 

            //用户

 

            if (radioButtonuser.Checked == true)

 

            {

 

                Dao dao = new Dao();

 

                string sql = $"select * from dbo.用户表 where 用户ID='{textBox1.Text}'and 用户密码='{textBox2.Text}'";

 

                IDataReader dc = dao.read(sql);

 

               if(dc.Read())

 

                {

 

                    Data.UID = dc["用户ID"].ToString();

 

                    Data.UName = dc["用户姓名"].ToString();

 

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

 

                    user1 user = new user1();//用户窗体进行实例化

 

                    this.Hide();                              //隐藏登录窗体

 

                    user.ShowDialog();                             //选择对话框

 

                    this.Show();//显示旧窗体

 

                }

 

                else

 

                {

 

                    MessageBox.Show("登陆失败!");

 

                }

 

                dao.DaoClose();

 

            }

 

            //管理员

 

            if (radioButtonadmin.Checked == true)

 

            {

 

                Dao dao = new Dao();

 

                string sql = $"select * from 管理员表 where 管理员ID='{textBox1.Text}'and 密码='{textBox2.Text}'";

 

                IDataReader dc = dao.read(sql);

 

                if (dc.Read())

 

                {

 

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

 

                    admin1 a = new admin1();

 

                    this.Hide();

 

                    a.ShowDialog();//用户窗体进行实例化

 

                    this.Show();               //显示旧窗体

 

                }

 

                else

 

                {

 

                    MessageBox.Show("登陆失败!");

 

    

 

                }

 

            }

 

            MessageBox.Show("单选框请先选中!");

 

        }

 

 

 

       

 

    }

 

}

 

//program.cs(C#页面)

 

using System;

 

using System.Windows.Forms;

 

 

 

namespace 图书管理系统1

 

{

 

    internal static class Program

 

    {

 

        /// <summary>

 

        /// 应用程序的主入口点。

 

        /// </summary>

 

        [STAThread]

 

        static void Main()

 

        {

 

            Application.EnableVisualStyles();

 

            Application.SetCompatibleTextRenderingDefault(false);

 

            Application.Run(new Form1());

 

           // Application.Run(new admin2());

 

        }

 

    }

 

}

 

//Data.cs(C#页面)

 

using System;

 

using System.Collections.Generic;

 

using System.Data;

 

using System.Linq;

 

using System.Text;

 

using System.Threading.Tasks;

 

 

 

namespace 图书管理系统1

 

{

 

   

 

    internal class Data//存储登陆的一些数据

 

    {

 

        public static string UID = "", UName = "";//用户ID和姓名默认为空

 

    }

 

 

 

}

 

//Dao.cs(C#页面)数据库连接代码

 

using System;

 

using System.Collections.Generic;

 

using System.Linq;

 

using System.Text;

 

using System.Threading.Tasks;

 

using System.Data.SqlClient;

 

using System.Runtime.InteropServices;

 

using System.ComponentModel;

 

using System.Security.Policy;

 

using System.Runtime.Remoting.Messaging;

 

using System.Runtime.InteropServices.WindowsRuntime;

 

using System.Data;

 

namespace 图书管理系统1

 

{

 

    class Dao//数据库连接

 

    {

 

        SqlConnection sc;

 

        public SqlConnection connect()

 

            {

 

                string str = @"Data Source=WIN-GIHP114UG9H;Initial Catalog=图书管理;Integrated Security=SSPI";//数据库连接字符串windons登录

 

                sc = new SqlConnection(str);//创建数据库

 

                sc.Open();//打开数据库

 

                return sc;//返回数据库连接对象

 

            }

 

            public SqlCommand command(string sql)//封装对数据库的操作

 

            {

 

                SqlCommand cmd = new SqlCommand(sql, connect());

 

                return cmd;

 

            }

 

            public int Execute(string sql)//更新操作

 

            {

 

                return command(sql).ExecuteNonQuery();

 

            }

 

            public SqlDataReader read(string sql)//读取操作

 

            {

 

                return command(sql).ExecuteReader();

 

            }

 

            public void DaoClose()

 

            {

 

                sc.Close();

 

            }

 

 

 

        }

 

    }

 

 

 

//登录管理员的主页面(admin1.cs窗口体系)

 

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 图书管理系统1

 

{

 

    public partial class admin1 : Form

 

    {

 

        public admin1()

 

        {

 

            InitializeComponent();

 

        }

 

        private void 图书管理ToolStripMenuItem_Click(object sender, EventArgs e)

 

        {

 

            admin2 admin = new admin2();

 

            admin.ShowDialog();//实现跳转

 

 

 

        }

 

    }

 

}

 

//管理员图书管理页面(admin2.cs窗口体系)

 

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 图书管理系统1

 

{

 

    public partial class admin2 : Form

 

    {

 

        public admin2()

 

        {

 

            InitializeComponent();

 

        }

 

 

 

        private void admin2_Load(object sender, EventArgs e)//页面

 

        {

 

            Table();

 

            label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString()+ dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//获取书号和书名

 

        }

 

        //从数据库读取数据显示在表格控件

 

        public void Table()

 

        {

 

            dataGridView1.Rows.Clear();//清空旧数据

 

            Dao dao = new Dao();

 

            string sql = "SELECT * FROM dbo.图书表";

 

            IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据

 

            while(dc.Read())//读完返回一个false跳出循环

 

            {

 

                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());

 

            }

 

            dc.Close();

 

            dao.DaoClose();

 

        }

 

        public void Table图书ID()//根据书号进行查询

 

        {

 

            dataGridView1.Rows.Clear();//清空旧数据

 

            Dao dao = new Dao();

 

            string sql = $"SELECT * FROM dbo.图书表 WHERE 图书ID='{textBox1.Text}'";

 

            IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据

 

            while (dc.Read())//读完返回一个false跳出循环

 

            {

 

                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());

 

            }

 

            dc.Close();

 

            dao.DaoClose();

 

        }

 

        public void Table图书名Name()//根据书名进行查询,模糊语句

 

        {

 

            dataGridView1.Rows.Clear();//清空旧数据

 

            Dao dao = new Dao();

 

            string sql = $"SELECT * FROM dbo.图书表 WHERE 图书名 like'%{textBox2.Text}%'";

 

            IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据

 

            while (dc.Read())//读完返回一个false跳出循环

 

            {

 

                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());

 

            }

 

            dc.Close();

 

            dao.DaoClose();

 

        }

 

        private void button3_Click(object sender, EventArgs e)//删除图书

 

        {

 

            try

 

            {

 

                string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取书号

 

                label2.Text = id + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();

 

                DialogResult dr = MessageBox.Show("确认删除吗?", "信息提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

 

                if (dr == DialogResult.OK)

 

                {

 

                    string sql = $"DELETE FROM dbo.图书表 WHERE 图书ID='{id}'";

 

                    Dao dao = new Dao();

 

                    if (dao.Execute(sql) > 0)

 

                    {

 

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

 

                        Table();

 

                    }

 

                    else

 

                    {

 

                        MessageBox.Show("删除失败!"+sql);

 

                    }

 

                    dao.DaoClose();

 

                }

 

            }

 

            catch

 

            {

 

                MessageBox.Show("请先在表格中选中要删除的图数记录!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Error);

 

            }

 

        }

 

 

 

        private void dataGridView1_Click(object sender, EventArgs e)//点击click选定要删除的图书

 

        {

 

            label2.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//获取书号和书名

 

        }

 

 

 

        private void button1_Click(object sender, EventArgs e)//添加图书

 

        {

 

            admin21 a = new admin21();

 

            a.ShowDialog();

 

        }

 

 

 

        private void button2_Click(object sender, EventArgs e)

 

        {

 

            try

 

            {

 

                string 图书ID=dataGridView1.SelectedRows[0].Cells[0].Value.ToString();

 

                string 图书名 = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();

 

                string 作者 = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();

 

                string 出版社 = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();

 

                string 库存 = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();

 

                admin22 admin = new admin22(图书ID, 图书名, 作者, 出版社, 库存);

 

                admin.ShowDialog();

 

                Table();//刷新数据

 

            }

 

            catch

 

            {

 

                MessageBox.Show("ERROR!");

 

            }

 

            

 

        }

 

 

 

        private void button5_Click(object sender, EventArgs e)

 

        {

 

            Table图书ID();

 

        }

 

 

 

        private void button6_Click(object sender, EventArgs e)

 

        {

 

            Table图书名Name();

 

        }

 

 

 

        private void button4_Click(object sender, EventArgs e)

 

        {

 

            Table();

 

            textBox1.Text = "";//文本框清空

 

            textBox1.Text = "";

 

        }

 

    }

 

}

//管理员图书管理——添加图书(admin21.cs窗口体系)

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 static System.Windows.Forms.VisualStyles.VisualStyleElement;

 

namespace 图书管理系统1

{

    public partial class admin21 : Form

    {

        public admin21()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "")

            {

                Dao dao = new Dao();

                string sql = $"INSERT INTO dbo.图书表 VALUES('{textBox1.Text}','{textBox2.Text}','{textBox3.Text}','{textBox4.Text}','{textBox5.Text}')";

                int n = dao.Execute(sql);

                if (n > 0)

                {

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

                }

                else

                {

                    MessageBox.Show("添加失败!");

                }

                textBox1.Text = "";

                textBox2.Text = "";

                textBox3.Text = "";

                textBox4.Text = "";

                textBox5.Text = "";

            }

            else

            {

                MessageBox.Show("输入不允许有空项!");

            }

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            textBox1.Text = "";

            textBox2.Text = "";

            textBox3.Text = "";

            textBox4.Text = "";

            textBox5.Text = "";

        }

    }

}

//管理员图书管理——修改图书(admin22.cs窗口体系)

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Net.NetworkInformation;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace 图书管理系统1

{

    public partial class admin22 : Form

    {

        string ID = "";//公用变量

        public admin22()

        {

            InitializeComponent();

        }

        public admin22(string 图书ID, string 图书名, string 作者, string 出版社, string 库存)

        {

            InitializeComponent();

            ID=textBox1.Text = 图书ID;

            textBox2.Text = 图书名;

            textBox3.Text = 作者;

            textBox4.Text = 出版社;

            textBox5.Text = 库存;

        }

 

        private void button1_Click(object sender, EventArgs e)//修改按钮的代码

        {

            string sql = $"UPDATE dbo.图书表 SET 图书ID='{textBox1.Text}',图书名='{textBox2.Text}',作者='{textBox3.Text}',出版社='{textBox4.Text}',库存='{textBox5.Text}' WHERE  图书ID='{ID}'";

            Dao dao = new Dao();

            if(dao.Execute(sql)>0)

            {

                MessageBox.Show("修改成功!");

                this.Close();

            }

 

        }

    }

}

//用户登录主页面(user1.cs窗口体系)

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 图书管理系统1

{

    public partial class user1 : Form

    {

        public user1()

        {

            InitializeComponent();

            label2.Text = $"欢迎{Data.UName}登录";

        }

 

        private void 图书查看和借阅ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            user2 a = new user2();//跳转页面

            a.ShowDialog();

        }

 

        private void 当前图书的借出和归还ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            user3 us = new user3();

            us.ShowDialog();

        }

 

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            MessageBox.Show("HELP!");

        }

 

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)

        {

            this.Close();

        }

    }

}

//用户图书管理——图书的查看和借阅(user2.cs窗口体系)

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 图书管理系统1

{

    public partial class user2 : Form

    {

        public user2()

        {

            InitializeComponent();

            Table();

        }

 

        private void user2_Load(object sender, EventArgs e)

        {

 

        }

        public void Table()

        {

            dataGridView1.Rows.Clear();//清空旧数据

            Dao dao = new Dao();

            string sql = "SELECT * FROM dbo.图书表";

            IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据

            while (dc.Read())//读完返回一个false跳出循环

            {

                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());

            }

            dc.Close();

            dao.DaoClose();

        }

 

        private void button1_Click(object sender, EventArgs e)//借出图书

        {

            string 图书ID = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//书号

            int 库存 = int.Parse(dataGridView1.SelectedRows[0].Cells[4].Value.ToString());//库存

            if(库存<1)

            {

                MessageBox.Show("库存不够,请联系管理员购入!");

            }

            else

            {

                string sql = $"INSERT INTO dbo.借书表([用户ID],[图书ID] ,[截止日期] )VALUES ('{Data.UID}','{图书ID}',GETDATE());UPDATE dbo.图书表 SET 库存=库存-1 WHERE 图书ID='{图书ID}'";

                Dao dao = new Dao();

                if(dao.Execute(sql)>1)//执行两条sql语句大于一才是都执行成功

                {

                    MessageBox.Show($"用户:{Data.UName}借出了图书{图书ID}!");

                    Table();

                }

                else

                {

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

                }

            }

        }

    }

}

//用户图书管理——我借到和归还的图书(user3.cs窗口体系)

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.Web;

using System.Windows.Forms;

 

namespace 图书管理系统1

{

    public partial class user3 : Form

    {

        public user3()

        {

            InitializeComponent();

            Table();

        }

        public void Table()

        {

            dataGridView1.Rows.Clear();//清空旧数据

            Dao dao = new Dao();

            string sql = $"SELECT [no],[图书ID],[截止日期] FROM dbo.借书表 WHERE 用户ID='{Data.UID}'";

            IDataReader dc = dao.read(sql);//把数据库的查询语句传到dao类read方法,dao类的read用来读取数据库,数据返回到dc,dc用来接收数据

            while (dc.Read())//读完返回一个false跳出循环

            {

                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString());//三个参数

            }

            dc.Close();

            dao.DaoClose();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            string 图书ID = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();//获取书号

            string no= dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//获取编号

            string sql = $"DELETE FROM dbo.借书表 WHERE [no]={no};UPDATE dbo.图书表 SET 库存=库存+1 WHERE 图书ID='{图书ID}'";

            Dao dao = new Dao();

            if(dao.Execute(sql)>1)

            {

                MessageBox.Show("归还成功!");

                Table();

            }

        }

    }

}

标签:管理系统,C#,Text,数据库,dao,System,dc,using,图书
From: https://www.cnblogs.com/shidawuyu/p/17027040.html

相关文章

  • C# 多线程学习笔记
     ///进程:程序在服务器上运行是,占据的计算资源合集,称之为进程;///进程之间不会相互干扰--进程之间的通信比较困难(分布式)///线程:程序执行的最小单位,相应操作的最小执......
  • docker 安装
    通过YUM进行docker的安装在centos系统中,我们一般通过yum进行软件包的安装。因此本次实验我们也通过yum来安装docker运行环境1、首先通过yum安装必要的依赖组件。为了编......
  • C#获得Access数据库中的表名,以及根据表名获取字段名
    ///<summary>///返回Mdb数据库中所有表表名///</summary>publicstring[]GetShemaTableName(stringdatabase_path,stringdatabase_password......
  • javascript array 数组 indexOf
    javascriptarray数组indexOf低版本的array是没有indexOf的。参考链接:https://reference.codeproject.com/javascript/Reference/Global_Objects/Array/indexOf/......
  • C#获得SQLServer服务器名、数据库名、表名、以及字段名
    usingSystem.Data.SqlClient;///<summary>///获取局域网内的所有数据库服务器名称///</summary>///<returns>服务器名称数组</ret......
  • VC中自行构建Stack(堆栈)类
    template<classT>classStack{structLink{T*data;Link*next;Link(T*dat,Link*nxt):data(dat),next(nxt){}}*head;public:Stack():head(0){}~Stack......
  • 运维之tmux 与 screen
    1.前提,我们之前都是在服务器nohup这个命令,让他在后端保持运行,但是那个有点不好,我们现在用tmux来玩1.1原理tmux==nohup2.安装tmuxyuminstall-ytmux3.......
  • C#实现treeview节点上下左右自由移动
    以下是节点移动类NodeMove.cs源码:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Collections;namespaceNo......
  • C#实现文件二进制存储
    privatestaticBinaryFormatterTransfer=newBinaryFormatter();publicintBinaryFileSav(){//-----二进制文件写入并存储......
  • C#实现TreeView向XML的绝对转换类
    从第一次接触XML开始就想写一个能实现tree和XML灵活转换的类了。写这个类大概用去了将近半天的时间,花的时间有些长了。呵呵。。好在收获颇多,熟练了XML的读写类,对C#中的forea......