-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrmUserManage.cs
More file actions
269 lines (227 loc) · 8.36 KB
/
Copy pathFrmUserManage.cs
File metadata and controls
269 lines (227 loc) · 8.36 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace RADARMRM
{
public partial class FrmUserManage : Form
{
private MssqlHelper m_pDbMSSql = null;
private ArrayList m_arryUsers = new ArrayList();
private int nCurId = -1;
public FrmUserManage(MssqlHelper p)
{
InitializeComponent();
m_pDbMSSql = p;
}
private void FrmUserManage_Load(object sender, EventArgs e)
{
InitList();
UserRole.Items.Add("管理员");
UserRole.Items.Add("操作员");
lbmsg.Text = "";
}
private void InitList()
{
if (m_pDbMSSql == null)
{
return;
}
string sSql = "SELECT * FROM BT_USER_REGISTER";
DataSet ds = m_pDbMSSql.GetDataSet(sSql);
if (ds == null) return;
if (ds.Tables.Count == 0) return;
if (ds.Tables[0].Rows.Count == 0) return;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataRow DR = ds.Tables[0].Rows[i];
LgUser lu = new LgUser();
lu.sIndex = DR[0].ToString();
lu.sId = DR[1].ToString();
lu.sPass = DR[2].ToString();
lu.sName = DR[3].ToString();
lu.sRole = DR[4].ToString();
m_arryUsers.Add(lu);
listBox1.Items.Add(DR[3].ToString() + "("+DR[1].ToString()+")");
}
}
private void button5_Click(object sender, EventArgs e)
{
this.Close();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int nSel = listBox1.SelectedIndex;
if (nSel < 0) return;
LgUser lu = (LgUser)m_arryUsers[nSel];
textBox1.Text = lu.sId;
textBox2.Text = lu.sName;
int m = Convert.ToInt32(lu.sRole);
if (m >= 0 && m < 3)
{
UserRole.SelectedIndex = m;
}
nCurId = nSel;
lbmsg.Text = "";
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
lbmsg.Text = "用户登录账号不能为空!";
return;
}
if (textBox2.Text == "")
{
lbmsg.Text = "请输入用户真实姓名!";
return;
}
if (UserRole.Text == "")
{
lbmsg.Text = "请选择用户角色!";
return;
}
lbmsg.Text = "正在保存用户信息...";
lbmsg.Refresh();
if (nCurId == -1)
{
string strSql = "INSERT INTO BT_USER_REGISTER(UserID,UserPWS,UserName,UserRole) VALUES('" + textBox1.Text + "','1234','" + textBox2.Text + "','" + UserRole.SelectedIndex.ToString() + "')";
if (m_pDbMSSql.ExecuteSql(strSql))
{
LgUser lu = new LgUser();
lu.sId = textBox1.Text;
lu.sName = textBox2.Text;
lu.sRole = UserRole.SelectedIndex.ToString();
m_arryUsers.Add(lu);
listBox1.Items.Add(lu.sName + "(" + lu.sId + ")");
lbmsg.Text = "完成创建新用户";
}
else
{
lbmsg.Text = "创建新用户失败";
}
}
else
{
int nSel = listBox1.SelectedIndex;
LgUser lu = (LgUser)m_arryUsers[nSel];
string strSql = "UPDATE BT_USER_REGISTER SET UserName='" + textBox2.Text + "',UserRole='" + UserRole.SelectedIndex.ToString() + "' WHERE ID=" + lu.sIndex;
if (m_pDbMSSql.ExecuteSql(strSql))
{
lu.sId = textBox1.Text;
lu.sName = textBox2.Text;
lu.sRole = UserRole.SelectedIndex.ToString();
listBox1.Items[nSel] = lu.sName + "(" + lu.sId + ")";
lbmsg.Text = "完成保存用户信息";
}
else
{
lbmsg.Text = "保存用户信息失败!";
}
}
}
private void button3_Click(object sender, EventArgs e)
{
string sUserId = "";
int m = 1000;
while (m-- == 0)
{
sUserId = GetRandomArray(9, 4);
if (TesttingUserID(sUserId))
break;
}
nCurId = -1;
textBox1.Text = GetRandomArray(9, 4);
textBox2.Text = "";
lbmsg.Text = "";
listBox1.SelectedIndex = -1;
}
private void button4_Click(object sender, EventArgs e)
{
int nSel = listBox1.SelectedIndex;
if (nSel < 0)
{
lbmsg.Text = "请选择要删除的用户...";
return;
}
if (MessageBox.Show("确定删除用户[" + listBox1.Text + "]?删除不可恢复", "信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
{
lbmsg.Text = "正在删除用户信息...";
lbmsg.Refresh();
LgUser lu = (LgUser)m_arryUsers[nSel];
string strSql = "DELETE FROM BT_USER_REGISTER WHERE ID=" + lu.sIndex;
if (m_pDbMSSql.ExecuteSql(strSql))
{
listBox1.Items.RemoveAt(nSel);
m_arryUsers.RemoveAt(nSel);
m_arryUsers.TrimToSize();
textBox1.Text = "";
textBox2.Text = "";
lbmsg.Text = "完成删除用户信息";
}
else
{
lbmsg.Text = "删除成败";
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int nSel = listBox1.SelectedIndex;
if (nSel < 0)
{
lbmsg.Text = "请选择需要初始化密码的用户...";
return;
}
if (MessageBox.Show("确定初始化用户[" + listBox1.Text + "]密码吗?", "信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk) == DialogResult.OK)
{
lbmsg.Text = "正在初始化密码...";
lbmsg.Refresh();
LgUser lu = (LgUser)m_arryUsers[nSel];
string strSql = "UPDATE BT_USER_REGISTER SET UserPWS='1234' WHERE ID=" + lu.sIndex;
if (!m_pDbMSSql.ExecuteSql(strSql))
{
lbmsg.Text = @"初始化密码失败!";
}
else
{
lbmsg.Text = "用户密码[" + listBox1.Text + "]初始为1234";
}
}
}
private string GetRandomArray(int maxNumber, int count)
{
string srt = "";
int[] array = new int[maxNumber];
for (int i = 0; i < maxNumber; i++)
array[i] = i + 1;
Random rnd = new Random();
for (int j = 0; j < count; j++)
{
int index = rnd.Next(j, maxNumber);
int temp = array[index];
srt += temp.ToString();
array[index] = array[j];
array[j] = temp;
}
return srt;
}
private bool TesttingUserID(string sUserId)
{
string strSql = "SELECT * FROM BT_USER_REGISTER WHERE UserID='" + sUserId + "'";
DataSet ds = m_pDbMSSql.GetDataSet(strSql);
if (ds == null) return true;
if (ds.Tables.Count == 0) return true;
if (ds.Tables[0].Rows.Count != 0) return false;
return true;
}
private void UserRole_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}