⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form2.cs

📁 图书馆管理系统data structure
💻 CS
字号:
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 图书馆管理系统
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.textBox1.Text == "" || this.textBox2.Text == "")
            {
                MessageBox.Show("借阅证号或密码不能为空!");
            }
            else
            {
                SqlConnection myConnection = new SqlConnection();
                String conStr = "server=localhost;integrated security=sspi;database=pubs";
                myConnection.ConnectionString = conStr;
                myConnection.Open();
                //确认该借阅证有效
                SqlCommand sc1= new SqlCommand("select islocked from users where id=" + this.textBox1.Text, myConnection);
                SqlDataReader sdr1= sc1.ExecuteReader();
                if (sdr1.Read())
                {
                    if (Convert.ToInt32(sdr1.GetValue(0)) == 1)
                    {
                        MessageBox.Show("该借阅证已无效!");
                        sdr1.Close();
                    }
                    else
                    {
                        sdr1.Close();
                        //确认该用户借阅的图书不到5本
                        SqlCommand sc2 = new SqlCommand("select*from states where returnDate='0' and uid='" + this.textBox1.Text + "'", myConnection);
                        SqlDataReader sdr2 = sc2.ExecuteReader();
                        int c = 0;
                        while (sdr2.Read())
                        {
                            c += 1;
                        }
                        if (c > 4)
                        {
                            sdr2.Close();
                            MessageBox.Show("您已经借阅了5本图书,请先归还至少一本图书!");

                        }
                        else
                        {
                            sdr2.Close();
                            SqlCommand sc3 = new SqlCommand("select islocked,num from books where bid='" + this.textBox2.Text + "'", myConnection);
                            SqlDataReader sdr3 = sc3.ExecuteReader();
                            if (sdr3.Read())
                            {
                                int num = Convert.ToInt32(sdr3.GetValue(1));
                                //确认该书可以外借
                                if (Convert.ToInt32(sdr3.GetValue(0)) == 0)
                                {
                                    sdr3.Close();
                                    SqlCommand sc4 = new SqlCommand("update books set num=" + Convert.ToString(num - 1) + "where bid='" + this.textBox2.Text + "'", myConnection);
                                    sc4.ExecuteNonQuery();
                                    String date = Convert.ToString(System.DateTime.Now);
                                    //添加借阅信息
                                    SqlCommand sc5 = new SqlCommand("insert into states values(" + Convert.ToInt32(this.textBox2.Text) + "," + Convert.ToInt32(this.textBox2.Text) + ",'" + date + "','" + 0 + "')", myConnection);
                                    sc5.ExecuteNonQuery();
                                    MessageBox.Show("借阅成功!");
                                    this.Close();
                                }
                                else
                                {
                                    sdr3.Close();
                                    MessageBox.Show("该书已被锁定,不允许外借!");
                                }
                            }
                            else
                            {
                                MessageBox.Show("该书编号不存在!");
                            }
                        }
                    }
                }
                else
                {
                    MessageBox.Show("该借阅证号不存在!");
                }
                myConnection.Close();
            }
        }

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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -