|
|
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 Form32 : Form
|
|
|
{
|
|
|
string SID,bookname,category;
|
|
|
public Form32(string sID,string bookname,string category)
|
|
|
{
|
|
|
SID = sID;
|
|
|
InitializeComponent();
|
|
|
Table(bookname,category);
|
|
|
}
|
|
|
public void Table(string bookname,string category) //此私有类是将数据加载到窗体中
|
|
|
{
|
|
|
dataGridView2.Rows.Clear(); //清除前面的数据,方便后面重新刷新数据
|
|
|
|
|
|
string sql = "select * from Book where Name like '%"+bookname+ "%' and category like '%" + category + "%'"; //这个逻辑是查找借阅表中的学号,然后找到学号对应的书籍,将书籍展示出来
|
|
|
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(); // 不用展示学号sid了
|
|
|
f = dr["Publisher"].ToString();
|
|
|
g = dr["Category"].ToString();
|
|
|
h = dr["State"].ToString();
|
|
|
string[] str = { a, b, c, d, e, f, g, h};
|
|
|
dataGridView2.Rows.Add(str); //添加数据
|
|
|
|
|
|
}
|
|
|
dr.Close();//关闭连接
|
|
|
}
|
|
|
private void 借阅此书ToolStripMenuItem_Click(object sender, EventArgs e)
|
|
|
{
|
|
|
string BookId = dataGridView2.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 = dataGridView2.SelectedCells[0].Value.ToString();
|
|
|
string Author = dataGridView2.SelectedCells[1].Value.ToString();
|
|
|
string Count = dataGridView2.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 dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|