-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfrmSQL.cs
More file actions
330 lines (299 loc) · 11 KB
/
frmSQL.cs
File metadata and controls
330 lines (299 loc) · 11 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using Microsoft.VisualBasic;
using SQL;
using System.Data;
using System.Formats.Asn1;
using System.Text;
using System.Xml.Linq;
using SecurityUtils;
namespace myproject
{
public partial class frmSQL : Form
{
string msg = string.Empty;
SQLHelper sh;
string base_sql = @"select studentNo as 学号, studentname as 姓名,CASE gender WHEN 1 THEN '男'
WHEN 0 THEN '女'
ELSE '其他'
END as 性别,Major as 专业 from tblTopStudents where 1=1";
/// <summary>
/// 构造函数往往放置一些初始化的工作
/// </summary>
public frmSQL()
{
InitializeComponent();
sh = new SQLHelper(); //数据库链接对象初始化
}
private void btnLink_Click(object sender, EventArgs e)
{
string sql = "select count(*) from tblTopStudents"; //该SQL意思是,获取tblstudents的行数
try
{
string? num = sh.RunSelectSQLToScalar(sql); //一般运行查询语句
msg = string.Format("我们班共有{0}个同学!", num);
}
catch (Exception ex)
{
msg = ex.Message;
}
finally
{
sh.Close();
}
MessageBox.Show(msg);
}
private void btnInsert_Click(object sender, EventArgs e)
{
string studentnumber = Interaction.InputBox("打卡", "请输入打卡的学号,默认是自己", "10130212110");
try
{
//string studentnumber = "10130212110";
string sql = string.Format("insert into tblStudentAbsent(studentNumber)values('{0}')", studentnumber);
int ret = sh.RunSQL(sql); //一般运行 查询之外的删、改、增
msg = string.Format("打卡成功");
}
catch (Exception ex)
{
msg = ex.Message;
}
finally
{
sh.Close();
}
MessageBox.Show(msg);
}
/// <summary>
/// 在初始化之后,内存中加载窗体的时候触发
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void frmSQL_Load(object sender, EventArgs e)
{
bindData(base_sql); //把数据绑定到datagridview
initCombox(); //初始化combobox
// 初始化 ImageList
ImageList imgList = new ImageList();
imgList.ColorDepth = ColorDepth.Depth32Bit;
imgList.ImageSize = new Size(32, 32); // 图片大小
// 加载图片到 ImageList
imgList.Images.Add(Image.FromFile(@"..\..\..\faces\boy.jpg"));
imgList.Images.Add(Image.FromFile(@"..\..\..\faces\girl.jpg"));
// 关联 ImageList 到 ListView
listView2.SmallImageList = imgList;
listView2.LargeImageList = imgList;
}
private void btnSearch_Click(object sender, EventArgs e)
{
string sql = base_sql;
string major = cboMajor.Text;
string name = txtStuName.Text;
if (name.Length > 0)
{
sql += string.Format(" and studentname like '%{0}%'", name);
}
if (cboMajor.Text != "全部显示")
{
sql += string.Format(" and major='{0}'", major);
}
bindData(sql);
}
/// <summary>
/// 给我传递一个SQL命令,我来绑定数据到datagridview
/// </summary>
/// <param name="sql">传递过来的SQL命令</param>
private void bindData(string sql)
{
//数据集 mini的database
DataSet ds = new DataSet();
try
{
// 函数名一样,参数不一样,这在OO里面叫重载 overload
sh.RunSQL(sql, ref ds);
//1个dataset包含落干个datatable
DataTable dt = ds.Tables[0];
dataGridView1.DataSource = dt;
lblCount.Text = string.Format("共有{0}个同学", dt.Rows.Count);
//dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ex)
{
msg = ex.Message;
MessageBox.Show(msg);
}
finally
{
sh.Close();
}
}
/// <summary>
/// 用tbltopstudents里面的major来初始化这个combox
/// </summary>
private void initCombox()
{
string sql = "SELECT DISTINCT major FROM tblTopStudents";
DataSet ds = new DataSet();
try
{
sh.RunSQL(sql, ref ds);
DataTable dt = ds.Tables[0];
dt.Rows.Add("全部显示");
// 绑定DataTable到ComboBox
cboMajor.DataSource = dt;
cboMajor.DisplayMember = "major";
// 如果你想将某个列作为值成员,可以这样设置:
cboMajor.ValueMember = "major";
cboMajor.Text = "全部显示";
}
catch (Exception ex)
{
msg = ex.Message;
MessageBox.Show(msg);
}
finally
{
sh.Close();
}
}
private void chkAll_CheckedChanged(object sender, EventArgs e)
{
bindData(base_sql); //把数据绑定到datagridview
}
private void button1_Click_1(object sender, EventArgs e)
{
timer1.Enabled = !timer1.Enabled;
if (timer1.Enabled)
btnAlert.Text = "停止报警";
else
btnAlert.Text = "开始报警";
}
private void timer1_Tick(object sender, EventArgs e)
{
SearchStu();
}
private void SearchStu()
{
listView1.Items.Clear();
listView2.Items.Clear();
string sql = "SELECT studentno,STUDENTNAME,gender FROM tblTopStudents\r\nWHERE studentNo NOT IN\r\n(\r\nselect STUDENTNUMBER from tblStudentAbsent where DATEDIFF(DAY,dtedate,GETDATE())=0\r\n)\r\n";
DataSet ds = new DataSet();
try
{
sh.RunSQL(sql, ref ds);
DataTable dt = ds.Tables[0];
bindImgTolist(dt);
}
catch (Exception ex)
{
msg = ex.Message;
MessageBox.Show(msg);
}
finally
{
sh.Close();
}
}
/// <summary>
/// 根据dt的信息更新两个listview 一个是更新文本名字,一个是更新图片
/// </summary>
/// <param name="dt"></param>
private void bindImgTolist(DataTable dt)
{
foreach (DataRow stu in dt.Rows)
{
ListViewItem item1 = new ListViewItem(stu[1].ToString());
if (stu[2].ToString() == "True")
item1.ImageIndex = 0; // 设置为 ImageList 中第一张图片
else
item1.ImageIndex = 1; // 设置为 ImageList 中第一张图片
listView2.Items.Add(item1);
StringBuilder tmp = new StringBuilder();
tmp.Append(string.Format("学号:{0},姓名:{1}", stu[0], stu[1]));
listView1.Items.Add(tmp.ToString());
}
}
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
// 确保点击的是数据行(行索引>=0)且不是标题行
if (e.RowIndex >= 0)
{
// 获取被点击的行
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
// 将行中的单元格值赋给文本框
// 注意:使用Cells[列索引]或Cells["列名"]来访问单元格
txtStudentname.Text = row.Cells["姓名"].Value.ToString();
txtStudentNo.Text = row.Cells["学号"].Value.ToString();
txtMajor.Text = row.Cells["专业"].Value.ToString();
if (row.Cells["性别"].Value.ToString() == "男")
pictureBox1.Image = Image.FromFile(@"..\..\..\faces\boy.jpg");
else
pictureBox1.Image = Image.FromFile(@"..\..\..\faces\girl.jpg");
// 如果某些列可能为null,可以使用安全转换
// 例如:txtAge.Text = row.Cells["Age"].Value?.ToString() ?? string.Empty;
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
try
{
//string studentnumber = "10130212110";
string sql = string.Format(@"update tblTopStudents
set Major='{0}',
LoginTimes=LoginTimes+1
where studentNo='{1}'", txtMajor.Text, txtStudentNo.Text);
int ret = sh.RunSQL(sql); //一般运行 查询之外的删、改、增
msg = string.Format("修改成功");
bindData(base_sql); //重新绑定数据
}
catch (Exception ex)
{
msg = ex.Message;
}
finally
{
sh.Close();
}
MessageBox.Show(msg);
}
private void txtStuName_TextChanged(object sender, EventArgs e)
{
}
private void btnResetPwd_Click(object sender, EventArgs e)
{
if(string.IsNullOrEmpty(txtStudentNo.Text))
{
MessageBox.Show("请先选择一个学生");
return;
}
if(txtPwd.Text.Length < 6) //检测密码强度,大模型求助
{
MessageBox.Show("密码长度不能小于6位");
return;
}
if(txtPwd.Text != txtConfirmPwd.Text)
{
MessageBox.Show("两次输入的密码不一致");
return;
}
// 创建哈希密码
string password = txtPwd.Text;
string salt;
string hash = PasswordHelper.CreateHash(password, out salt); //不可逆哈希
try
{
string sql = string.Format(@"update tblTopStudents
set pwd='{0}', salt='{1}'
where studentNo='{2}'", hash, salt, txtStudentNo.Text);
int ret = sh.RunSQL(sql); //一般运行 查询之外的删、改、增
msg = string.Format("重置密码成功!");
}
catch (Exception ex)
{
msg = ex.Message;
}
finally
{
sh.Close();
}
MessageBox.Show(msg);
}
}
}