




已阅读5页,还剩23页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
UserHelper.cs类代码using System;using System.Collections.Generic;using System.Text;namespace MyQQ /记录登录的用户Id class UserHelper public static int loginId; /登录的用户Id using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace MyQQ / / 聊天窗体 / public partial class ChatForm : Form public int friendId; / 当前聊天的好友号码 public string nickName; / 当前聊天的好友昵称 public int faceId; / 当前聊天的好友头像Id public ChatForm() InitializeComponent(); / 窗体加载时的动作 private void ChatForm_Load(object sender, EventArgs e) / 设置窗体标题 this.Text = string.Format(与0聊天中,nickName); / 设置窗体顶部显示的好友信息 picFace.Image = ilFaces.ImagesfaceId; lblFriend.Text = string.Format(0(1),nickName,friendId); / 读取所有的未读消息,显示在窗体中 ShowMessage(); / 关闭窗体 private void btnClose_Click(object sender, EventArgs e) this.Close(); / 发送消息 private void btnSend_Click(object sender, EventArgs e) if (txtChat.Text.Trim() = ) / 不能发送空消息 MessageBox.Show(不能发送空消息!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); return; else if (txtChat.Text.Trim().Length 50) MessageBox.Show(消息内容过长,请分为几条发送!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); return; else / 发送消息,写入数据库 / MessageTypeId:1-表示聊天消息,为简化操作没有读取数据表,到S2可以用常量或者枚举实现 / MessageState:0-表示消息状态是未读 int result = -1; / 表示操作数据库的结果 string sql = string.Format( INSERT INTO Messages (FromUserId, ToUserId, Message, MessageTypeId, MessageState) VALUES (0,1,2,3,4), UserHelper.loginId, friendId, txtChat.Text.Trim(), 1, 0); try / 执行命令 SqlCommand command = new SqlCommand(sql, DBHelper.connection); DBHelper.connection.Open(); result = command.ExecuteNonQuery(); catch (Exception ex) Console.WriteLine(ex.Message); finally DBHelper.connection.Close(); if (result != 1) MessageBox.Show(服务器出现意外错误!, 抱歉, MessageBoxButtons.OK, MessageBoxIcon.Error); txtChat.Text = ; / 输入消息清空 this.Close(); / / 读取所有的未读消息,显示在窗体中 / private void ShowMessage() string messageIdsString = ; / 消息的Id组成的字符串 string message; / 消息内容 string messageTime; / 消息发出的时间 / 读取消息的SQL语句 string sql = string.Format( SELECT Id, Message,MessageTime From Messages WHERE FromUserId=0 AND ToUserId=1 AND MessageTypeId=1 AND MessageState=0, friendId,UserHelper.loginId); try SqlCommand command = new SqlCommand(sql, DBHelper.connection); DBHelper.connection.Open(); SqlDataReader reader = command.ExecuteReader(); / 循环将消息添加到窗体上 while (reader.Read() messageIdsString += Convert.ToString(readerId) + _; message = Convert.ToString(readerMessage); messageTime = Convert.ToDateTime(readerMessageTime).ToString(); / 转换为日期类型,告诉学员 lblMessages.Text += string.Format(n0 1n 2,nickName,messageTime,message); reader.Close(); catch (Exception ex) Console.WriteLine(ex.Message); finally DBHelper.connection.Close(); / 把显示出的消息置为已读 if (messageIdsString.Length 1) messageIdsString.Remove(messageIdsString.Length - 1); SetMessageRead(messageIdsString, _); / / 把显示出的消息置为已读 / private void SetMessageRead(string messageIdsString, char separator) string messageIds = messageIdsString.Split(separator); / 分割出每个消息Id string sql = Update Messages SET MessageState=1 WHERE Id=; / 更新状态的SQL语句的固定部分 string updateSql; / 执行的SQL语句 try SqlCommand command = new SqlCommand(); / 创建Command对象 command.Connection = DBHelper.connection; / 指定数据库连接 DBHelper.connection.Open(); / 打开数据库连接 foreach (string id in messageIds) if (id != ) updateSql = sql + id; / 补充完整的SQL语句 command.CommandText = updateSql; / 指定要执行的SQL语句 int result = command.ExecuteNonQuery(); / 执行命令 catch (Exception ex) Console.WriteLine(ex.Message); finally DBHelper.connection.Close(); ChatForm.cs代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace MyQQ / / 聊天窗体 / public partial class ChatForm : Form public int friendId; / 当前聊天的好友号码 public string nickName; / 当前聊天的好友昵称 public int faceId; / 当前聊天的好友头像Id public ChatForm() InitializeComponent(); / 窗体加载时的动作 private void ChatForm_Load(object sender, EventArgs e) / 设置窗体标题 this.Text = string.Format(与0聊天中,nickName); / 设置窗体顶部显示的好友信息 picFace.Image = ilFaces.ImagesfaceId; lblFriend.Text = string.Format(0(1),nickName,friendId); / 读取所有的未读消息,显示在窗体中 ShowMessage(); / 关闭窗体 private void btnClose_Click(object sender, EventArgs e) this.Close(); / 发送消息 private void btnSend_Click(object sender, EventArgs e) if (txtChat.Text.Trim() = ) / 不能发送空消息 MessageBox.Show(不能发送空消息!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); return; else if (txtChat.Text.Trim().Length 50) MessageBox.Show(消息内容过长,请分为几条发送!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); return; else / 发送消息,写入数据库 / MessageTypeId:1-表示聊天消息,为简化操作没有读取数据表,到S2可以用常量或者枚举实现 / MessageState:0-表示消息状态是未读 int result = -1; / 表示操作数据库的结果 string sql = string.Format( INSERT INTO Messages (FromUserId, ToUserId, Message, MessageTypeId, MessageState) VALUES (0,1,2,3,4), UserHelper.loginId, friendId, txtChat.Text.Trim(), 1, 0); try / 执行命令 SqlCommand command = new SqlCommand(sql, DBHelper.connection); DBHelper.connection.Open(); result = command.ExecuteNonQuery(); catch (Exception ex) Console.WriteLine(ex.Message); finally DBHelper.connection.Close(); if (result != 1) MessageBox.Show(服务器出现意外错误!, 抱歉, MessageBoxButtons.OK, MessageBoxIcon.Error); txtChat.Text = ; / 输入消息清空 this.Close(); / / 读取所有的未读消息,显示在窗体中 / private void ShowMessage() string messageIdsString = ; / 消息的Id组成的字符串 string message; / 消息内容 string messageTime; / 消息发出的时间 / 读取消息的SQL语句 string sql = string.Format( SELECT Id, Message,MessageTime From Messages WHERE FromUserId=0 AND ToUserId=1 AND MessageTypeId=1 AND MessageState=0, friendId,UserHelper.loginId); try SqlCommand command = new SqlCommand(sql, DBHelper.connection); DBHelper.connection.Open(); SqlDataReader reader = command.ExecuteReader(); / 循环将消息添加到窗体上 while (reader.Read() messageIdsString += Convert.ToString(readerId) + _; message = Convert.ToString(readerMessage); messageTime = Convert.ToDateTime(readerMessageTime).ToString(); / 转换为日期类型,告诉学员 lblMessages.Text += string.Format(n0 1n 2,nickName,messageTime,message); reader.Close(); catch (Exception ex) Console.WriteLine(ex.Message); finally DBHelper.connection.Close(); / 把显示出的消息置为已读 if (messageIdsString.Length 1) messageIdsString.Remove(messageIdsString.Length - 1); SetMessageRead(messageIdsString, _); / / 把显示出的消息置为已读 / private void SetMessageRead(string messageIdsString, char separator) string messageIds = messageIdsString.Split(separator); / 分割出每个消息Id string sql = Update Messages SET MessageState=1 WHERE Id=; / 更新状态的SQL语句的固定部分 string updateSql; / 执行的SQL语句 try SqlCommand command = new SqlCommand(); / 创建Command对象 command.Connection = DBHelper.connection; / 指定数据库连接 DBHelper.connection.Open(); / 打开数据库连接 foreach (string id in messageIds) if (id != ) updateSql = sql + id; / 补充完整的SQL语句 command.CommandText = updateSql; / 指定要执行的SQL语句 int result = command.ExecuteNonQuery(); / 执行命令 catch (Exception ex) Console.WriteLine(ex.Message); finally DBHelper.connection.Close(); private void lblMessages_Click(object sender, EventArgs e) DBHelper.cs类代码using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;namespace MyQQ / 数据库帮助类,维护数据库连接字符串和数据库连接对象 class DBHelper private static string connString = Data Source=.;database=MyQQ;integrated security=sspi; public static SqlConnection connection = new SqlConnection(connString); using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace MyQQ / / 头像选择窗体 / public partial class FacesForm : Form public PersonalInfoForm personalInfoForm; / 个人信息窗体 public FacesForm() InitializeComponent(); 1.FacesForm.cs代码 private void FacesForm_Load(object sender, EventArgs e) for (int i = 0; i ilFaces.Images.Count; i+) lvFaces.Items.Add(i.ToString(); lvFaces.Itemsi.ImageIndex = i; / 确定选择头像 private void btnOK_Click(object sender, EventArgs e) if (lvFaces.SelectedItems.Count = 0) MessageBox.Show(请选择一个头像!, 提示, MessageBoxButtons.OK, MessageBoxIcon.Information); else int faceId = lvFaces.SelectedItems0.ImageIndex; / 获得当前选中的头像的索引 personalInfoForm.ShowFace(faceId); / 设置个人信息窗体中显示的头像 this.Close(); / 双击时选择头像 private void lvIcons_MouseDoubleClick(object sender, MouseEventArgs e) int faceId = lvFaces.SelectedItems0.ImageIndex; / 获得当前选中的头像的索引 personalInfoForm.ShowFace(faceId); / 设置个人信息窗体中显示的头像 this.Close(); / 关闭窗体 private void btnCancel_Click(object sender, EventArgs e) this.Close(); LoginForm.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace MyQQ / / 登录窗体 / public partial class LoginForm : Form public LoginForm() InitializeComponent(); / 取消按钮的事件处理 private void btnCancel_Click(object sender, EventArgs e) Application.Exit(); / 打开申请号码界面 private void llbl_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) RegisterForm registerForm = new RegisterForm(); registerForm.Show(); / 登录按钮事件处理 private void btnLogin_Click(object sender, EventArgs e) bool error = false; / 标志在执行数据库操作的过程中是否出错 / 如果输入验证成功,就验证身份,并转到相应的窗体 if (ValidateInput() int num = 0; / 数据库操作结果 try / 查询用的sql语句 string sql = string.Format(SELECT COUNT(*) FROM Users WHERE Id=0 AND LoginPwd = 1, int.Parse(txtLoginId.Text.Trim(), txtLoginPwd.Text.Trim(); / 创建Command 对象 SqlCommand command = new SqlCommand(sql, DBHelper.connection); DBHelper.connection.Open(); / 打开数据库连接 num = Convert.ToInt32(command.ExecuteScalar(); catch (Exception ex) error = true; Console.WriteLine(ex.Message); finally DBHelper.connection.Close(); / 关闭数据库连接 if (!error & (num = 1) / 验证通过 / 设置登录的用户号码 UserHelper.loginId = int.Parse(txtLoginId.Text.Trim(); / 创建主窗体 MainForm mainForm = new MainForm(); mainForm.Show(); / 显示窗体 this.Visible = false; / 当前窗体不可见 else MessageBox.Show(输入的用户名或密码有误!, 登录提示, MessageBoxButtons.OK, MessageBoxIcon.Error); / 忘记密码标签 private void llblFogetPwd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) MessageBox.Show(该功能尚未开通!,提示,MessageBoxButtons.OK,MessageBoxIcon.Information); / 用户输入验证 private bool ValidateInput() / 验证用户输入 if (txtLoginId.Text.Trim() = ) MessageBox.Show(请输入登录的号码, 登录提示, MessageBoxButtons.OK, MessageBoxIcon.Information); txtLoginId.Focus(); return false; else if (txtLoginPwd.Text.Trim() = ) MessageBox.Show(请输入密码, 登录提示, MessageBoxButtons.OK, MessageBoxIcon.Information); txtLoginPwd.Focus(); return false; return true; MainForm.csusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using Aptech.UI;using System.Data.SqlClient;u
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论