窗体应用程序防腾讯QQ源码
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using Aptech.UI;
9 using System.Data.SqlClient;
10 using System.Media;
11
12 namespace MyQQ
13 {
14 public partial class MainForm : Form
15 {
16 int fromUserId; // 消息的发起者
17 int friendFaceId; // 发消息的好友的头像Id
18 int messageImageIndex = 0; // 工具栏中的消息图标的索引
19 public static int faceId = 0; // 头像索引
20 public MainForm()
21 {
22 InitializeComponent();
23 }
24
25 private void MainForm_Load(object sender, EventArgs e)
26 {
27 //初始化皮肤
28 skinEngine1.SkinFile = "SportsGreen.ssk";
29 // 显示个人的信息
30 timSound.Start();
31 wenti();
32 sbFriends.AddGroup("我的好友");
33 sbFriends.AddGroup("陌生人");
34 //this.sideBar1.AddGroup("我的好友");
35 //this.sideBar1.AddGroup("陌生人");
36
37 //this.sideBar1.Groups[0].Items.Add("小红1", 1);
38 //this.sideBar1.Groups[0].Items.Add("小红2", 1);
39 //this.sideBar1.Groups[0].Items.Add("小红3", 1);
40
41 //SubItem sit = new SbItem("小花", 80);
42 //sit.Tag = 1000;
43 //this.sideBar1.Groups[0].Items.Add(sit);
44 //查看好友
45 ADDName();
46
47 this.Left = Screen.PrimaryScreen.WorkingArea.Width-this.Width;
48 //this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Width;
49 //this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Height;
50
51 }
52
53 private void ADDName()
54 {
55 //清空信息
56 sbFriends.Groups[0].Items.Clear();
57 //创建sql语句(实现查询)
58 string sql = string.Format(
59 "SELECT FriendId,NickName,FaceId FROM Users,Friends WHERE Friends.HostId={0} AND Users.Id=Friends.FriendId",
60 UserHelper.loginId);
61 //执行try块语句
62 try
63 {
64 //创建command对象
65 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
66 if (DBHelper.connection.State!= ConnectionState.Open)
67 {
68 DBHelper.connection.Open();//打开数据库连接
69 }
70 //使用变量reader来存取执行命令后的值
71 SqlDataReader reader = command.ExecuteReader();
72 //循环使用Read()方法
73 while (reader.Read())
74 {
75 //创建item对象(使用到ListView控件)
76 SbItem item = new SbItem((string)reader["NickName"], (int)reader["FaceId"]);
77 item.Tag = reader["FriendId"];//使用Tag临时变量来存取FrienId该列的值
78 sbFriends.Groups[0].Items.Add(item);//执行显示(从下标0开始)
79
80 }
81 reader.Close();//关闭reader()
82 }
83 catch (Exception ex)
84 {
85 //提示错误信息
86 MessageBox.Show(ex.Message);
87 }
88 finally
89 {
90 //关闭数据库连接
91 DBHelper.connection.Close();
92 }
93 }
94 private string wenti()
95 {
96 string nickName = ""; // 昵称
97
98 bool error = false; // 标识是否出现错误
99
100 // 取得当前用户的昵称、头像(实现查询)
101 string sql = string.Format(
102 "SELECT NickName, FaceId FROM Users WHERE Id={0}",
103 UserHelper.loginId);
104 //执行try块语句
105 try
106 {
107 // 创建command对象
108 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
109 //打开数据库连接
110 DBHelper.connection.Open();
111 //执行command命令
112 SqlDataReader dataReader = command.ExecuteReader();
113 //循环使用Read()方法
114 if (dataReader.Read())
115 {
116 if (!(dataReader["NickName"] is DBNull)) // 判断数据库类型是否为空
117 {
118 nickName = Convert.ToString(dataReader["NickName"]);
119 }
120 //使用变量faceId来存取数据类型转换后的值
121 faceId = Convert.ToInt32(dataReader["FaceId"]);
122 }
123 //关闭dataReader()
124 dataReader.Close();
125 }
126 catch (Exception ex)
127 {
128 //提示错误信息
129 error = true;
130 Console.WriteLine(ex.Message);
131 }
132 finally
133 {
134 //关闭数据库连接
135 DBHelper.connection.Close();
136 }
137
138 // 根据操作数据库结果进行不同的操作
139 if (error)
140 {
141 //提示用户信息
142 MessageBox.Show("服务器请求失败!请重新登录!", "意外错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
143 Application.Exit();//退出当前应用程序
144 }
145 else
146 {
147 // 在窗体标题显示登录的昵称、号码
148 this.Text = UserHelper.loginId.ToString();
149 this.picboName.Image = imglstBig.Images[faceId];
150 this.label1.Text = string.Format("{0}({1})", nickName, UserHelper.loginId.ToString());
151
152 }
153 return sql;
154 }
155
156 private void tsmiView_Click(object sender, EventArgs e)
157 {
158 if (sbFriends.View == SbView.LargeIcon)
159 {
160 sbFriends.View = SbView.SmallIcon;
161 tsmiView.Text = "显示大图像";
162 }
163 else if (sbFriends.View == SbView.SmallIcon)
164 {
165 sbFriends.View = SbView.LargeIcon;
166 tsmiView.Text = "显示小图像";
167 }
168 }
169
170 private void sbFriends_ItemDoubleClick(SbItemEventArgs e)
171 {
172 ChatForm chat = new ChatForm();
173 chat.friendId = Convert.ToInt32(e.Item.Tag); // 号码
174 chat.nickName = e.Item.Text; // 昵称
175 chat.faceId = e.Item.ImageIndex; // 头像
176 timer2.Stop();//点击收取信息后图片停止换取
177 timer1.Stop();
178 chat.Show();
179 }
180
181 private void picboName_Click(object sender, EventArgs e)
182 {
183 PersonalInfoForm face = new PersonalInfoForm();
184 face.Show();
185 }
186
187 private void timer1_Tick(object sender, EventArgs e)
188 {
189
190
191
192 }
193
194 private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
195 {
196 Application.Exit();
197 }
198
199 private void toolStripMenuItem3_Click(object sender, EventArgs e)
200 {
201 skinEngine1.SkinFile = "SportsGreen.ssk";
202 label1.ForeColor =Color.BlueViolet;
203 sbFriends.ForeColor = Color.BlanchedAlmond;
204 sbFriends.GroupHeaderBackColor = Color.Green;
205 sbFriends.BackColor = Color.SpringGreen;
206 }
207
208 private void 黑色ToolStripMenuItem_Click(object sender, EventArgs e)
209 {
210 skinEngine1.SkinFile = "SteelBlack.ssk";
211 label1.ForeColor = Color.White;
212 sbFriends.ForeColor = Color.Black;
213 sbFriends.GroupHeaderBackColor = Color.Black;
214 sbFriends.BackColor = Color.Gray;
215 }
216
217 private void 橙色ToolStripMenuItem_Click(object sender, EventArgs e)
218 {
219 skinEngine1.SkinFile = "SportsOrange.ssk";
220 label1.ForeColor = Color.Black;
221 sbFriends.ForeColor = Color.White;
222 sbFriends.GroupHeaderBackColor = Color.Orange;
223 sbFriends.BackColor = Color.HotPink;
224 }
225
226 private void toolStripButton2_Click(object sender, EventArgs e)
227 {
228
229 SearchFriendForm Search = new SearchFriendForm();
230 Search.Show();
231 }
232
233 private void tsbSound_Click(object sender, EventArgs e)
234 {
235
236 timer1.Stop(); // 消息timer停止运行
237 // 图片恢复正常
238 messageImageIndex = 0;
239 tsbSound.Image = imageList1.Images[messageImageIndex];
240
241 string sql = string.Format("select FromUserId from dbo.Messages where MessageState = 0 and MessageTypeId = 2 and ToUserId ={0}", UserHelper.loginId);
242
243
244 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
245 DBHelper.connection.Open();
246 int FromUserId = Convert.ToInt32(command.ExecuteScalar());
247
248 DBHelper.connection.Close();
249 RequestForm request = new RequestForm();
250 request.Show();
251
252 }
253 //系统信息,喇叭闪动
254 private void timer1_Tick_1(object sender, EventArgs e)
255 {
256 messageImageIndex = messageImageIndex == 0 ? 1 : 0;//如果messageImageIndex为0就将1赋给它,否则就将0赋给他
257 tsbSound.Image = imageList1.Images[messageImageIndex];
258
259 SoundPlayer soundPlay = new SoundPlayer("system.wav");
260 soundPlay.Play();
261
262 }
263 /// <summary>
264 /// 读取数据库消息启动计时器
265 /// </summary>
266 /// <param name="sender"></param>
267 /// <param name="e"></param>
268 private void timSound_Tick(object sender, EventArgs e)
269 {
270
271 }
272 /// <summary>
273 /// 更新陌生人列表
274 /// </summary>
275 private void UpdateStranger(int loginId)
276 {
277 // 选出这个人的基本信息
278 string sql = "SELECT NickName, FaceId FROM Users WHERE Id=" + loginId;
279 bool error = false; // 用来标识是否出现错误
280 try
281 {
282 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
283 DBHelper.connection.Open();
284 SqlDataReader dataReader = command.ExecuteReader(); // 查询
285 if (dataReader.Read())
286 {
287 SbItem item = new SbItem((string)dataReader["NickName"], (int)dataReader["FaceId"]);
288 item.Tag = this.fromUserId; // 将Id记录在Tag属性中
289 sbFriends.Groups[1].Items.Add(item); // 向陌生人组中添加项
290 }
291 sbFriends.VisibleGroup = sbFriends.Groups[1]; // 设定陌生人组为可见组
292 }
293 catch (Exception ex)
294 {
295 error = true;
296 Console.WriteLine(ex.Message);
297 }
298 finally
299 {
300 DBHelper.connection.Close();
301 }
302
303 // 出错了
304 if (error)
305 {
306 MessageBox.Show("服务器出现意外错误!", "抱歉", MessageBoxButtons.OK, MessageBoxIcon.Error);
307 }
308 }
309 /// <summary>
310 /// 判断发消息的人是否在列表中
311 /// </summary>
312 private bool HasShowUser(int loginId)
313 {
314 bool find = false; // 表示是否在当前显示出的用户列表中找到了该用户
315
316 // 循环 SideBar 中的2个组,寻找发消息的人是否在列表中
317 for (int i = 0; i < 2; i++)
318 {
319 for (int j = 0; j < sbFriends.Groups[i].Items.Count; j++)
320 {
321 if (Convert.ToInt32(sbFriends.Groups[i].Items[j].Tag) == loginId)
322 {
323 find = true;
324 }
325 }
326 }
327 return find;
328 }
329
330 private void timer2_Tick(object sender, EventArgs e)
331 {
332 // 循环好友列表两个组中的每个item,找到发消息的好友,让他的头像闪烁
333 for (int i = 0; i < 2; i++)
334 {
335 for (int j = 0; j < sbFriends.Groups[i].Items.Count; j++)
336 {
337 if (Convert.ToInt32(sbFriends.Groups[i].Items[j].Tag) == this.fromUserId)
338 {
339 if (sbFriends.Groups[i].Items[j].ImageIndex < 100)
340 {
341 sbFriends.Groups[i].Items[j].ImageIndex = 100;// 索引为100的图片是一个空白图片
342 }
343 else
344 {
345 sbFriends.Groups[i].Items[j].ImageIndex = this.friendFaceId;
346 }
347 sbFriends.Invalidate(); // 重新绘制,只要告诉学生需要这句话才能正常闪烁头像就行
348
349 }
350 }
351 }
352 SoundPlayer sound = new SoundPlayer("msg.wav");
353 sound.Play();
354
355 }
356
357 private void sbFriends_Load(object sender, EventArgs e)
358 {
359
360 }
361
362 private void timSound_Tick_1(object sender, EventArgs e)
363 {
364 ADDName(); // 刷新好友列表
365 int messageTypeId = 1; // 消息类型
366 int messageState = 1; // 消息状态
367
368 // 找出未读消息对应的好友Id
369 string sql = string.Format(
370 "SELECT Top 1 FromUserId, MessageTypeId, MessageState FROM Messages WHERE ToUserId={0} AND MessageState=0", UserHelper.loginId);
371 SqlCommand command;
372 try
373 {
374 command = new SqlCommand(sql, DBHelper.connection);
375 if (DBHelper.connection.State != ConnectionState.Open)
376 {
377 DBHelper.connection.Open();
378 }
379 SqlDataReader dataReader = command.ExecuteReader();
380
381 // 循环读出一个未读消息
382 if (dataReader.Read())
383 {
384 this.fromUserId = (int)dataReader["FromUserId"];
385 messageTypeId = (int)dataReader["MessageTypeId"];
386 messageState = (int)dataReader["MessageState"];
387 }
388 dataReader.Close();
389 }
390 catch (Exception ex)
391 {
392 Console.WriteLine(ex.Message);
393 }
394 finally
395 {
396 DBHelper.connection.Close();
397 }
398
399 // 判断消息类型,如果是添加好友消息,就启动喇叭timer,让小喇叭闪烁
400 if (messageTypeId == 2 && messageState == 0)
401 {
402 // SoundPlayer player = new SoundPlayer("system.wav");
403 //player.Play();
404 timer1.Start();//启动闪喇叭的计时器
405 }
406 // 如果是聊天消息,就启动聊天timer,让好友头像闪烁
407 else if (messageTypeId == 1 && messageState == 0)
408 {
409 // 获得发消息的人的头像Id
410 sql = "SELECT FaceId FROM Users WHERE Id=" + this.fromUserId;
411 try
412 {
413 command = new SqlCommand(sql, DBHelper.connection);
414 DBHelper.connection.Open();
415 this.friendFaceId = Convert.ToInt32(command.ExecuteScalar()); // 查找到发消息的好友的头像索引
416 }
417 catch (Exception ex)
418 {
419 Console.WriteLine(ex.Message);
420 }
421 finally
422 {
423 DBHelper.connection.Close();
424 }
425
426 // 如果发消息的人没有在好友列表中就添加到陌生人列表中
427 if (!HasShowUser(fromUserId))
428 {
429 UpdateStranger(fromUserId);
430 }
431 // SoundPlayer player = new SoundPlayer("msg.wav");
432 //player.Play();
433 timer2.Start(); // 启动闪烁头像定时器
434 }
435 }
436
437 private void toolStripButton1_Click(object sender, EventArgs e)
438 {
439 PersonalInfoForm person = new PersonalInfoForm();
440 person.Show();
441 }
442
443 private void toolStripButton5_Click(object sender, EventArgs e)
444 {
445 Application.Exit();
446 }
447
448 private void update_Tick(object sender, EventArgs e)
449 {
450 //查看好友
451 ADDName();
452 wenti();
453 }
454
455 private void tsmiDelete_Click(object sender, EventArgs e)
456 {
457 string FriendId = sbFriends.SeletedItem.Tag.ToString();
458 sbFriends.SeletedItem.Parent.Items.Remove(sbFriends.SeletedItem);
459
460 string sql = string.Format("delete from dbo.Friends where HostId={0} and FriendId={1}", UserHelper.loginId, FriendId);
461 SqlCommand com = new SqlCommand(sql, DBHelper.connection);
462 DBHelper.connection.Open();
463 int reader = com.ExecuteNonQuery();
464 if (reader == 1)
465 {
466 MessageBox.Show("成功");
467 }
468 else
469 {
470 MessageBox.Show("失败");
471 }
472
473 DBHelper.connection.Close();
474 }
475
476 private void tsb_Click(object sender, EventArgs e)
477 {
478 ADDName();
479 wenti();
480 }
481
482 private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
483 {
484 DialogResult res = MessageBox.Show("你确定关吗?", "温馨提示:", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
485 if (res == DialogResult.OK)
486 {
487 e.Cancel = false;
488 }
489 else
490 {
491 e.Cancel = true;
492 }
493 }
494
495 private void timer3_Tick(object sender, EventArgs e)
496 {
497 headForm();
498 }
499
500 private void headForm()
501 {
502 string nickName = "";
503 // 取得当前用户的昵称、头像
504 string sql = string.Format(
505 "SELECT NickName, FaceId FROM Users WHERE Id={0}",
506 UserHelper.loginId);
507 try
508 {
509 // 查询
510 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
511 DBHelper.connection.Open();
512 SqlDataReader dataReader = command.ExecuteReader();
513
514 if (dataReader.Read())
515 {
516
517 nickName = Convert.ToString(dataReader["NickName"]);
518
519 faceId = Convert.ToInt32(dataReader["FaceId"]);
520 }
521 dataReader.Close();
522 }
523 catch (Exception ex)
524 {
525
526 Console.WriteLine(ex.Message);
527 }
528 finally
529 {
530 DBHelper.connection.Close();
531 }
532
533
534 // 在窗体标题显示登录的昵称、号码
535
536 this.picboName.Image = imglstBig.Images[faceId];
537 }
538
539 private void 沉默是金ToolStripMenuItem_Click(object sender, EventArgs e)
540 {
541 skinEngine1.SkinFile = "MSN.ssk";
542 }
543
544 private void MainForm_MouseHover(object sender, EventArgs e)
545 {
546
547 }
548
549 private void MainForm_MouseMove(object sender, MouseEventArgs e)
550 {
551 //this.Left = Screen.PrimaryScreen.WorkingArea.Width-20;
552
553 }
554 private void MainForm_MouseEnter(object sender, EventArgs e)
555 {
556 // if (this.Left == Screen.PrimaryScreen.WorkingArea.Width - 10)
557 // {
558 //this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width - 100;
559 // }
560 // else
561 // {
562 // this.Top = Screen.PrimaryScreen.WorkingArea.Height - 10;
563 //}
564
565
566
567
568 }
569
570 private void MainForm_MouseLeave(object sender, EventArgs e)
571 {
572
573 }
574
575 private void MainForm_MouseClick(object sender, MouseEventArgs e)
576 {
577
578 }
579 }
580 }
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Data.SqlClient;
9 namespace MyQQ
10 {
11 public partial class ChatForm : Form
12 {
13 public int friendId; // 当前聊天的好友号码
14 public string nickName; // 当前聊天的好友昵称
15 public int faceId; // 当前聊天的好友头像Id
16 string messageIdsString = ""; // 消息的Id组成的字符串
17 string message; // 消息内容
18 string messageTime; // 消息发出的时间
19 public ChatForm()
20 {
21 InitializeComponent();
22 }
23
24 private void ChatForm_Load(object sender, EventArgs e)
25 {
26
27 this.Text = string.Format("与{0}聊天中...", nickName); // 设置窗体标题
28 lblFriend.Text = string.Format("{0}({1})", nickName, friendId); //当前聊天的好友昵称和号码
29 picFace.Image = ilFace.Images[faceId];
30
31 // 读取所有的未读消息,显示在窗体中
32 Showview();
33 }
34
35 private void Showview()
36 {
37 string str = string.Format(
38 "SELECT Id, Message,MessageTime From Messages WHERE FromUserId={0} AND ToUserId={1} AND MessageTypeId=1 AND MessageState=0",
39 friendId, UserHelper.loginId);
40
41 try
42 {
43 SqlCommand com = new SqlCommand(str, DBHelper.connection);
44 DBHelper.connection.Open();
45 SqlDataReader reader = com.ExecuteReader();
46 while (reader.Read())
47 {
48 messageIdsString += Convert.ToString(reader["Id"]) + "_";
49 message = Convert.ToString(reader["Message"]);
50 messageTime = Convert.ToDateTime(reader["MessageTime"]).ToString();
51 txtMessage.Text += string.Format("\n{0} {1}\n {2}", nickName, messageTime, message);
52 }
53 reader.Close();
54 }
55 catch (Exception ex)
56 {
57
58 MessageBox.Show(ex.Message);
59 }
60 finally
61 {
62 DBHelper.connection.Close();
63 }
64
65 if (messageIdsString.Length > 1) //查看Id是否有效
66 {
67 messageIdsString.Remove(messageIdsString.Length - 1);
68 NewMethod(messageIdsString, '_');
69 }
70 }
71
72 private void NewMethod(string messageIdsString, char separator)
73 {
74 string[] chai = messageIdsString.Split(separator);
75 string sql = "update Messages set MessageState=1 where Id=";
76 string updateSql;
77 try
78 {
79 SqlCommand com = new SqlCommand();
80
81 com.Connection = DBHelper.connection;
82 DBHelper.connection.Open();
83 foreach (string id in chai)
84 {
85 if (id != "")
86 {
87 updateSql = sql + id;
88 com.CommandText = updateSql;
89 int result = com.ExecuteNonQuery();
90 }
91 }
92 }
93 catch (Exception ex)
94 {
95 MessageBox.Show(ex.Message);
96
97 }
98 finally
99 {
100 DBHelper.connection.Close();
101 }
102 }
103
104 private void btnClose_Click(object sender, EventArgs e)
105 {
106 this.Close();
107 }
108 private void btnSend_Click(object sender, EventArgs e)
109 {
110 //判断txtWrit该控件是否为空,如是提示用户信息
111 if (txtWrit.Text.Trim() == "")
112 {
113 MessageBox.Show("请输入信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
114 return;
115 }
116 //判断数组长度是否大于等于150,如果大于等于150其实用户信息
117 else if (txtWrit.Text.Trim().Length >= 150)
118 {
119 MessageBox.Show("此消息过长!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
120 return;
121 }
122 //否则,执行try块语句(实现增加信息)
123 else
124 {
125 try
126 {
127 //创建sql语句实现增加信息
128 string sql = string.Format(
129 "insert into Messages(FromUserId,ToUserId,Message,MessageTypeId,MessageState) values({0},{1},'{2}',1,0)",
130 UserHelper.loginId, friendId, txtWrit.Text.Trim());
131 //创建command对象
132 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
133 //打开数据库连接
134 DBHelper.connection.Open();
135 //执行非查询(增加)命令
136 command.ExecuteNonQuery();
137 }
138 catch (Exception ex)
139 {
140 //提示错误信息
141 MessageBox.Show(ex.Message);
142 }
143 finally
144 {
145 //关闭数据库连接
146 DBHelper.connection.Close();
147 }
148 }
149
150 }
151
152 }
153 }
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Data.SqlClient;//导入命名空间
9 namespace MyQQ
10 {
11 public partial class FaceForm : Form
12 {
13
14 public FaceForm()
15 {
16 InitializeComponent();
17 }
18
19 private void butfaceNO_Click(object sender, EventArgs e)
20 {
21 DialogResult result = MessageBox.Show("确定要退出吗?","温馨提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question);
22 if (result==DialogResult.OK)
23 {
24 this.Close();//执行关闭当前窗体语句块
25 }
26
27 }
28
29 private void butfaceOK_Click(object sender, EventArgs e)
30 {
31 //判断是否选中图像,如果未选中图像点击确定的时候提示用户信息是否退出
32 if (listface.SelectedItems.Count==0)
33 {
34 DialogResult result= MessageBox.Show("确定要退出吗?","温馨提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
35 if (result == DialogResult.OK)
36 {
37 this.Close();//执行关闭当前窗体
38 }
39 }
40 else
41 {
42 //使用变量faceIds来存取选中的该图片
43 int faceIds = listface.SelectedItems[0].ImageIndex;
44
45
46 try
47 {
48 //创建sql语句(更新语句)
49 string sql = string.Format(
50 "update Users set FaceId={0} where Id={1}",
51 faceIds, UserHelper.loginId);
52 //创建command对象
53 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
54 //打开数据库连接
55 DBHelper.connection.Open();
56 //执行非查询命令(更新)
57 int result = command.ExecuteNonQuery();
58 if (result > 0)
59 {
60 MessageBox.Show("操作成功!");//提示用户信息操作成功
61 }
62 else
63 {
64 MessageBox.Show("操作失败!");//提示用户信息操作失败
65 }
66 }
67 catch (Exception ex)
68 {
69
70 MessageBox.Show(ex.Message);//提示错误信息
71 }
72 finally
73 {
74 DBHelper.connection.Close();//关闭数据库连接
75 }
76
77 }
78
79 }
80
81 private void FaceForm_Load(object sender, EventArgs e)
82 {
83 //执行在加载的时候显示信息(ListView控件)
84 for (int i = 0; i < ilFaces.Images.Count; i++)
85 {
86 //执行增加项语句块(从下标为0开始进行显示)
87 listface.Items.Add(i.ToString());
88 listface.Items[i].ImageIndex = i;
89
90
91 }
92 }
93 }
94 }
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Data.SqlClient;//导入命名空间
9 namespace MyQQ
10 {
11 public partial class RegisterForm : Form
12 {
13 string sex = "";//声明一个成员变量并且初始化
14
15
16 public RegisterForm()
17 {
18 InitializeComponent();
19 }
20
21 private void button2_Click(object sender, EventArgs e)
22 {
23 this.Close();
24 }
25
26 private void button1_Click(object sender, EventArgs e)
27 {
28
29 if (rbt.Checked)
30 {
31 sex="男";
32 Register();
33 }
34 else
35 {
36 sex="女";
37 Register();
38 }
39
40 }
41
42 private void Register()
43 {
44 if (tbopwd1.Text == tbopwd.Text)
45 {
46 int star = 0;
47 int boo = 0;
48 switch (BooldStyle.Text)
49 {
50 case "A型":
51 boo = 1;
52 break;
53 case "B型":
54 boo = 2;
55 break;
56 case "O型":
57 boo = 3;
58 break;
59 default:
60 boo = 4;
61 break;
62 }
63 switch (Star.Text)
64 {
65 case "白羊座":
66 star = 1;
67 break;
68 case "双子座":
69 star = 3;
70 break;
71 case "巨蟹座":
72 star = 4;
73 break;
74 case "狮子座":
75 star = 5;
76 break;
77 case "处女座":
78 star = 6;
79 break;
80 case "天秤座":
81 star = 7;
82 break;
83 case "天蝎座":
84 star = 8;
85 break;
86 case "射手座":
87 star = 9;
88 break;
89 case "摩羯座":
90 star = 10;
91 break;
92 case "水瓶座":
93 star = 11;
94 break;
95 case "双鱼座":
96 star = 12;
97 break;
98 default:
99 star = 2;
100 break;
101 }
102
103 try
104 {
105 string sql = string.Format("insert into dbo.Users(Age,Sex,LoginPwd,NickName,Name,StarId,BloodTypeId) values({0},'{1}','{2}','{3}','{4}','{5}',{6})", tboAge.Text, sex, tbopwd.Text, tboName.Text, ming.Text, star, boo);
106 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
107 DBHelper.connection.Open();
108 int result = command.ExecuteNonQuery();
109 if (result == 1)
110 {
111 //查询新增加的记录的标识号的代码
112 sql = "select @@Identity from Users";
113 command.CommandText = sql; //新增指定command对象的SQL语句
114
115 //执行查询语句
116 int num = Convert.ToInt32(command.ExecuteScalar());
117 string a = string.Format("注册成功!你的MyQQ号是{0}", num);
118 MessageBox.Show(a, "注册成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
119 this.Close();
120 }
121
122
123 }
124 catch (Exception ex)
125 {
126
127 MessageBox.Show(ex.Message);
128 }
129 finally
130 {
131 DBHelper.connection.Close();
132 }
133 }
134 else
135 {
136 MessageBox.Show("你的两次密码输入不相同!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
137
138 }
139 }
140
141
142 }
143 }
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Data.SqlClient;
9 namespace MyQQ
10 {
11 public partial class SearchFriendForm : Form
12 {
13 bool a = false;//声明一个布尔型的变量a并初始值设为false
14 private DataSet dataset = new DataSet();//创建datasSet对象
15 private SqlDataAdapter adapter;//声明变量adaper
16
17 public SearchFriendForm()
18 {
19 InitializeComponent();
20 }
21
22 /// <summary>
23 /// 点击查找的事件
24 /// </summary>
25 private void btnSerch_Click_1(object sender, EventArgs e)
26 {
27 panel1.Visible = true;//将panel1设为true(显示)
28 //创建sql语句(实现查询)
29 string sql = "select Id,NickName,Age,Sex from Users";
30 //给变量赋值
31 adapter = new SqlDataAdapter(sql, DBHelper.connection);
32 //使用SqlDataAdaper对象的Fill()方法进行填充
33 adapter.Fill(dataset);
34 //执行显示(下标从0开始)
35 dtVsearch.DataSource = dataset.Tables[0];
36
37
38
39 if (tabSearch.SelectedIndex == 0)
40 {
41
42 //精确查找
43 rigorSearch();
44 }
45 else
46 {
47 //高级查找
48 AdvancedSearch();
49
50 }
51 }
52 /// <summary>
53 /// 点击加为好友的事件
54 /// </summary>
55 private void btnAdd_Click(object sender, EventArgs e)
56 {
57 //要加朋友的QQ号码
58 int friendId = searchID();
59 //判断是否已被加
60 a = HasAdded(friendId);
61
62 //好友策略
63 //count等于1:允许任何人加我为好友
64 //count等于2:需要身份验证才能加我为好友
65 //count等于3:不允许任何人加我为好友
66 int count = tactic(friendId);
67 if (friendId==UserHelper.loginId)
68 {
69 MessageBox.Show("不可以加自己为好友");
70 }
71 //可以直接加
72 else if (a == false && count == 1)
73 {
74 MessageBox.Show("添加成功");
75 //添加操作
76 addFriendId(friendId);
77
78 }
79 else if (a == false && count == 2)
80 {
81 MessageBox.Show("对方需要验证,验证消息已发出");
82 if (friendId!=UserHelper.loginId)
83 {
84 tacticres(friendId);
85 }
86 //验证添加
87 }
88 else if (a == true)
89 {
90 MessageBox.Show("已是你的好友了");
91 }
92 else
93 {
94 MessageBox.Show("添加失败,对方不应许任何人加为好友");
95 }
96 }
97 /// <summary>
98 /// 点击上一步的事件
99 /// </summary>
100 private void btnBack_Click(object sender, EventArgs e)
101 {
102 this.panel1.Visible = false;//将panel1初始设为false (不可见)
103 }
104 /// <summary>
105 /// 点击关闭的事件
106 /// </summary>
107 private void butoff_Click(object sender, EventArgs e)
108 {
109 this.Close();//关闭当前窗体
110 }
111 /// <summary>
112 /// 点击关闭的事件
113 /// </summary>
114 private void btnClose_Click(object sender, EventArgs e)
115 {
116 this.Close();//关闭当前窗体
117 }
118
119 //高级查找方法
120 private void AdvancedSearch()
121 {
122 //创建sql语句(查询语句)
123 string sql = "";
124 sql = "SELECT Id,NickName,Age,Sex FROM Users";
125 //声明变量初始化和使用变量来存取对应的值
126 string termComAge = "";
127 string termComSex = comSex.Text;
128 //使用switch结构来实现年龄在某个范围内进行运用
129 switch (comAge.SelectedIndex)
130 {
131 case 1:
132 termComAge = " Age>=0 AND Age<10";
133 break;
134 case 2:
135 termComAge = " Age>=10 AND Age<20";
136 break;
137 case 3:
138 termComAge = " Age>=20 AND Age<30";
139 break;
140 case 4:
141 termComAge = " Age>=30 AND Age<40";
142 break;
143 case 5:
144 termComAge = " Age>=40 AND Age<50";
145 break;
146 case 6:
147 termComAge = " Age>=50";
148 break;
149 default:
150 termComAge = "";
151 break;
152 }
153 //判断如果这两个控件的值如果都为空的话,提示用户相应的信息
154 if (termComAge == "" && termComSex == "")
155 {
156 MessageBox.Show("你还没有选择的啊??");
157 return;
158 }
159 //判断如果这两个控件的值如果不为空的话,进行相应的计算
160 else
161 {
162 if (termComAge != "" && termComSex == "")
163 {
164 sql += string.Format(" where {0}", termComAge);
165 }
166 else if (termComAge == "" && termComSex != "")
167 {
168 sql += string.Format(" where Sex='{0}'", termComSex);
169 }
170 else
171 {
172 sql += string.Format(" where {0} AND Sex='{1}'", termComAge, termComSex);
173 }
174
175 }
176 //清空表中的信息
177 dataset.Tables[0].Clear();
178 //给变量赋值
179 adapter = new SqlDataAdapter(sql, DBHelper.connection);
180 //使用SqldataAdaper对象的fill()方法进行填充
181 adapter.Fill(dataset);
182 //执行显示语句块(并且下标从0开始)
183 dtVsearch.DataSource = dataset.Tables[0];
184 }
185 //精确查找方法
186 private void rigorSearch()
187 {
188 //创建sql语句块(实现查询)
189 string sql = "";
190
191 sql = "SELECT Id,NickName,Age,Sex FROM Users";
192
193 if (radexactitude.Checked == true)
194 {
195 if (friendQQ.Text == "" && friendName.Text == "")
196 {
197
198 //MessageBox.Show("你还没有填条件的啊@ !@");
199 }
200 else if (friendQQ.Text != "" && friendName.Text == "")
201 {
202 sql += string.Format(" where ID={0}", int.Parse(friendQQ.Text.Trim()));
203 }
204 else
205 {
206 sql += string.Format(" where NickName like '%{0}%'", friendName.Text);
207 }
208 }
209 //清空表中的信息
210 dataset.Tables[0].Clear();
211 //给变量赋值
212 adapter = new SqlDataAdapter(sql, DBHelper.connection);
213 //使用SqldataAdaper对象的fill()方法进行填充
214 adapter.Fill(dataset);
215 //执行显示语句块(并且下标从0开始)
216 dtVsearch.DataSource = dataset.Tables[0];
217 }
218 //发送验证请求方法
219 private int tacticres(int friendId)
220 {
221 int result = 0; // 操作结果
222 string sql = string.Format("INSERT INTO Messages (FromUserId, ToUserId, MessageTypeId, MessageState) VALUES ({0},{1},{2},{3})",
223 UserHelper.loginId, friendId, 2, 0);
224 try
225 {
226 // 执行添加操作
227 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
228 //打开数据库连接
229 DBHelper.connection.Open();
230 //使用变量result来存取执行命令后的值
231 result = command.ExecuteNonQuery();
232 if(result>0)
233 {
234 MessageBox.Show("操作成功!");//提示用户信息操作成功
235 }
236 else
237 {
238 MessageBox.Show("操作失败!");//提示用户信息操作失败
239 }
240 }
241 catch (Exception ex)
242 {
243 //提示错误信息
244 MessageBox.Show(ex.Message);
245 }
246 finally
247 {
248 //关闭数据库连接
249 DBHelper.connection.Close();
250 }
251 return result;
252 }
253 //添加操作方法
254 private int addFriendId(int friendId)
255 {
256 int result = 0;
257
258 DBHelper.connection.Open();
259 try
260 {
261 string sql = string.Format("insert into dbo.Friends(HostId,FriendId)values ({0},{1})", UserHelper.loginId, friendId);
262 //创建command对象
263 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
264 //执行命令(使用变量result来存取执行后的值)
265 result = command.ExecuteNonQuery();
266 }
267 catch (Exception ex)
268 {
269 //提示错误信息
270 MessageBox.Show(ex.Message);
271 }
272 finally
273 {
274 //关闭数据库连接
275 DBHelper.connection.Close();
276 }
277 return result;
278 }
279 //好友策略方法
280 private int tactic(int friendId)
281 {
282
283 int count = 0;//声明变量count并且初始化
284 try
285 {
286 //创建sql语句块(实现查询)
287 string sql = string.Format("select FriendshipPolicyId from dbo.Users where id={0}", friendId);
288 //创建command对象
289 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
290 //打开数据库连接
291 DBHelper.connection.Open();
292 //执行命令(并且使用变量count来存取,在之前也要进行转换为整型)
293 count = int.Parse(command.ExecuteScalar().ToString());
294
295
296 }
297 catch (Exception ex)
298 {
299 //提示错误信息
300 MessageBox.Show(ex.Message);
301 }
302 finally
303 {
304 //关闭数据库连接
305 DBHelper.connection.Close();
306 }
307 return count;
308 }
309 //获取要加的朋友的QQ号码方法
310 private int searchID()
311 {
312 int friendId = 0;
313
314 //当没有选中任何一行
315 if (dtVsearch.SelectedRows.Count == 0)
316 {
317 MessageBox.Show("请选择一个好友!");
318 }
319 //确保第一个单元格有值
320 else if (dtVsearch.SelectedRows[0].Cells[0] != null)
321 {
322 //获得第一个单元格的值
323 friendId = int.Parse(dtVsearch.SelectedRows[0].Cells[0].Value.ToString());
324
325 }
326 return friendId;
327 }
328 //是否已被加的方法
329 public bool HasAdded(int friendId)
330 {
331 //创建sql语句(实现查询)
332 string sql = string.Format("select id from dbo.Friends where HostId ={0} and FriendId={1}", UserHelper.loginId, friendId);
333 //打开数据库连接
334 DBHelper.connection.Open();
335 //执行try块语句
336 try
337 {
338 SqlCommand command = new SqlCommand(sql, DBHelper.connection);
339 string reader = Convert.ToString(command.ExecuteScalar());
340 //不可以加
341 if (reader != "")
342 {
343 this.a = true;
344 }
345 //可以加
346 else
347 {
348 this.a = false;
349 }
350
351 }
352 catch (Exception ex)
353 {
354 //提示错误信息
355 Console.WriteLine(ex.Message);
356 }
357 finally
358 {
359 //关闭数据库连接
360 DBHelper.connection.Close();
361
362 }
363 return a;
364 }
365
366 private void SearchFriendForm_Load(object sender, EventArgs e)
367 {
368
369 }
370
371
372
373
374
375
376 }
377 }