-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser2.cs
200 lines (182 loc) · 5.54 KB
/
user2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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 BookManagementSystem
{
public partial class User2 : Form
{
public User2()
{
InitializeComponent();
}
private void user2_Load(object sender, EventArgs e)
{
Table();
}
// 从数据库读取数据显示在表格控件中
public void Table()
{
// 把控件中的旧数据清空
dataGridView1.Rows.Clear();
Dao dao = new Dao();
string sql = "SELECT * FROM t_book";
IDataReader dc = dao.Read(sql);
while (dc.Read())
{
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 number = int.Parse(dataGridView1.SelectedRows[0].Cells[4].Value.ToString());
if (overtime() > 0)
{
int n = overtime();
MessageBox.Show($"您当前有{n}本书籍超时未还,请先归还并缴纳{n * 50}元罚金!");
return;
}
if (isFull())
{
MessageBox.Show("您借书数目已达上限");
return;
}
if (1 > number)
{
MessageBox.Show("库存不足,请联系管理员");
}
else
{
string sql =
$"INSERT INTO t_lend " +
$"([uid], bid, [datetime]) " +
$"VALUES (" +
$"'{Data.UID}', " +
$"'{id}', " +
$"getdate()" +
$"); " +
$"UPDATE t_book " +
$"SET " +
$"number = number - 1 " +
$"WHERE " +
$"id = " +
$"'{id}'";
Dao dao = new Dao();
// > 1 : 两条 SQL 语句都执行成功
if (1 < dao.Execute(sql))
{
MessageBox.Show($"用户:{Data.UName} 借出了图书{id}");
Table();
}
}
}
// 书号查询
private void button5_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Dao dao = new Dao();
string sql = $"SELECT * FROM t_book WHERE id = '{textBox1.Text}'";
IDataReader dc = dao.Read(sql);
while (dc.Read())
{
dataGridView1.Rows.Add(
dc[0].ToString(),
dc[1].ToString(),
dc[2].ToString(),
dc[3].ToString(),
dc[4].ToString()
);
}
dc.Close();
dao.DaoClose();
}
// 书名查询
private void button6_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Clear();
Dao dao = new Dao();
string sql = $"SELECT * FROM t_book WHERE name LIKE '%{textBox2.Text}%'";
IDataReader dc = dao.Read(sql);
while (dc.Read())
{
dataGridView1.Rows.Add(
dc[0].ToString(),
dc[1].ToString(),
dc[2].ToString(),
dc[3].ToString(),
dc[4].ToString()
);
}
dc.Close();
dao.DaoClose();
}
// 刷新
private void button2_Click(object sender, EventArgs e)
{
Table();
}
// 判断是否达到上限
private bool isFull()
{
bool isFull = false;
Dao d = new Dao();
string query =
$"SELECT COUNT(*) " +
$"FROM t_lend " +
$"WHERE uid = {Data.UID}";
IDataReader dr = d.Read(query);
if (dr.Read())
{
if (Data.UDept == "教职工")
{
if ((int)dr[0] == 10)
{
isFull = true;
}
}
if (Data.UDept == "学生")
{
if ((int)dr[0] == 15)
{
isFull = true;
}
}
}
return isFull;
}
// 超期图书数
private int overtime()
{
int result = 0;
Dao dao = new Dao();
string sql =
$"SELECT COUNT(*) " +
$"FROM t_lend " +
$"WHERE " +
$"uid = '{Data.UID}' " +
$"AND " +
$"DATEDIFF(mm, [datetime], getdate()) > 2";
IDataReader dr = dao.Read(sql);
if (dr.Read())
{
result = (int)dr[0];
}
return result;
}
}
}