|
|
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 Sql
|
|
|
{
|
|
|
public partial class Form3 : Form
|
|
|
{
|
|
|
string SID;
|
|
|
public Form3(string sID)
|
|
|
{
|
|
|
SID = sID;
|
|
|
InitializeComponent();
|
|
|
toolStripStatusLabel2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
toolStripStatusLabel1.Text = "欢迎学号为" + SID + "的同学登录图书借阅系统";
|
|
|
timer1.Start();
|
|
|
Table();
|
|
|
}
|
|
|
public void Table() //此私有类是将数据加载到窗体中
|
|
|
{
|
|
|
dataGridView1.Rows.Clear(); //清除前面的数据,方便后面重新刷新数据
|
|
|
string sql = "select * from Book";
|
|
|
Dao dao = new Dao();
|
|
|
IDataReader dr = dao.read(sql);
|
|
|
while (dr.Read())
|
|
|
{
|
|
|
string a, b, c, d, e,f,g,h;
|
|
|
a = dr["Id"].ToString(); //这例可以自己修改
|
|
|
b = dr["Name"].ToString();
|
|
|
c = dr["Author"].ToString();
|
|
|
d = dr["DOP"].ToString();
|
|
|
e = dr["Count"].ToString();
|
|
|
f = dr["Publisher"].ToString();
|
|
|
g = dr["Category"].ToString();
|
|
|
h = dr["State"].ToString();
|
|
|
string[] str = { a, b, c, d, e,f,g,h};
|
|
|
dataGridView1.Rows.Add(str); //添加数据
|
|
|
}
|
|
|
dr.Close();//关闭连接
|
|
|
}
|
|
|
|
|
|
private void timer1_Tick(object sender, EventArgs e)
|
|
|
{
|
|
|
toolStripStatusLabel2.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
}
|
|
|
|
|
|
private void Form3_FormClosed(object sender, FormClosedEventArgs e)
|
|
|
{
|
|
|
Application.Exit(); //***非常重要:上面的Form3_FormClosed就是Form3右上方的那个叉叉,单击后整个程序及所有窗口都会关闭
|
|
|
}
|
|
|
|
|
|
private void 借阅ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
string BookId = dataGridView1.SelectedCells[0].Value.ToString();//获取选中的图书号
|
|
|
string sql1 = "select * from BookRecord where sID='" + SID + "'and BookId='" + BookId + "'";//查询是否已经借阅了这本书
|
|
|
Dao dao = new Dao();
|
|
|
IDataReader dc = dao.read(sql1); //IDataReader是什么意思?是可以进行数据库操作的对象的意思
|
|
|
DialogResult r = MessageBox.Show("是否确认借阅", "提示", MessageBoxButtons.OKCancel);
|
|
|
|
|
|
if (!dc.Read() && r == DialogResult.OK)
|
|
|
{
|
|
|
string sql5 = "select Count from BookRecord where sID='" + SID + "'";//查询该同学借阅了多少书籍
|
|
|
Dao dao5 = new Dao();
|
|
|
IDataReader dc5 = dao.read(sql5);
|
|
|
dc5.Read();
|
|
|
string bcount = dc5["Count"].ToString();//bcount借过的图书数量
|
|
|
string s2 = "5";
|
|
|
|
|
|
if (s2.CompareTo(bcount) == 1)//5和借阅数量比较,首先5大,然后成立,bcount+1,一直加假如5=5,那么不给再继续借了
|
|
|
{
|
|
|
|
|
|
|
|
|
//这里要加一个查询book表的代码
|
|
|
string BId = dataGridView1.SelectedCells[0].Value.ToString();
|
|
|
string Author = dataGridView1.SelectedCells[1].Value.ToString();
|
|
|
string Count = dataGridView1.SelectedCells[4].Value.ToString();
|
|
|
string jytime = DateTime.Now.ToString("yyyy - MM - dd HH: mm:ss");
|
|
|
string ghtime = DateTime.Now.AddMonths(3).ToString("yyyy - MM - dd HH: mm:ss");
|
|
|
string sql2 = "update Book set Count = '" + Count + "' - 1 WHERE Id = '" + BId + "' ";
|
|
|
int i2 = dao.Execute(sql2);
|
|
|
|
|
|
if (i2 > 0)//图书数量大于的话就执行下面的
|
|
|
{
|
|
|
string sql = "insert into BookRecord(BookId,BookName,jytime,ghtime ,SID ) values('" + BId + "','" + Author + "','" + jytime + "','" + ghtime + "','" + SID + "')";
|
|
|
int i = dao.Execute(sql);
|
|
|
string sql3 = "UPDATE BookRecord SET Count = Count+1 WHERE SID = " + SID + " ";
|
|
|
int i3 = dao.Execute(sql3);//更新图书记录表,给相应用户的图书数量+1
|
|
|
MessageBox.Show("借阅成功");
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
MessageBox.Show("图书库存不足!");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
MessageBox.Show("借阅数量已达五本,请先还书再借阅!");
|
|
|
}
|
|
|
}
|
|
|
else if (!dc.Read() && r == DialogResult.Cancel)
|
|
|
{
|
|
|
//无需任何操作
|
|
|
}
|
|
|
/* else if(i2 < 0)//这个就是i2没有执行成功后来到这里
|
|
|
{
|
|
|
MessageBox.Show("图书库存不足");
|
|
|
}*/
|
|
|
else
|
|
|
{
|
|
|
MessageBox.Show("你已经借阅了该图书!");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void Form3_Load(object sender, EventArgs e)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
private void 我的借阅ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
Form31 f = new Form31(SID);
|
|
|
f.Show(); //打开form31窗口
|
|
|
}
|
|
|
|
|
|
private void 搜索图书ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
string B = textBox1.Text;
|
|
|
string C = comboBox1.Text;//图书类型
|
|
|
Form32 f = new Form32(SID, B, C);
|
|
|
f.Show(); //打开form32窗口
|
|
|
}
|
|
|
private void 退出当前账号ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
DialogResult hh = MessageBox.Show(" 是否确定退出当前账号", "提示", MessageBoxButtons.OKCancel);
|
|
|
if(hh == DialogResult.OK)
|
|
|
{
|
|
|
Form1 form1 = new Form1();
|
|
|
form1.Show();
|
|
|
this.Hide();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
//无需任何操作
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|