伤城文章网 > IT/计算机 > MyQQ聊天程序设计

MyQQ聊天程序设计


聊天程序详细设计
小组成员表
组长: 组长:魏杰 成员: 成员:魏杰 周兰 验收项目: 验收项目:仿 QQ 聊天程序 一. 建立数据库 创建 MyQQ 数据库 建立用户表“UserLog 的表“MessageLog UserLog 中有 xm(姓名) ,ID(账号) ,pw(密码) ,sq(权限) ,sj(时间) MessageLog 中有 fz(发送) ,sz(收收) ,xx(信息) ,sj(时间)

二、 基本框架 开始界面

登录界面

查看好友界面:

聊天界面:

取回密码界面:

注册界面:

注册成功界面:

具体代码实现见附件源代码 二. 实现数据库连接 添加 Data 类

using System; using System.Collections.Generic; using System.Linq; using System.Text;

using System.Data.SqlClient; using System.Data; namespace MyQQ { class Data { private static string constring = "server=.;database=MyQQ;uid=sa;pwd="; private static SqlConnection sqlcon = new SqlConnection(); private static SqlCommand sqlcom = new SqlCommand(); private static void openConnection() // 打开连接 { if (sqlcon.State == ConnectionState.Closed) { try { sqlcon.ConnectionString = constring; sqlcom.Connection = sqlcon; sqlcon.Open(); } catch (Exception e) { throw new Exception(e.Message); } } } private static void closeConnection()// 关闭连接 { if (sqlcon.State == ConnectionState.Open) { sqlcon.Close(); } sqlcon.Dispose(); sqlcom.Dispose(); } public static void ExecuteSql(string sqlStr)// 执行一条sql语句 { try { openConnection(); sqlcom.CommandType = CommandType.Text; sqlcom.CommandText = sqlStr; sqlcom.ExecuteNonQuery(); } catch (Exception e)

{ throw new Exception(e.Message); } finally { closeConnection(); } } public static DataSet ExecuteDataSet(string sqlStr)// 执行一条sql语句 { try { openConnection(); sqlcom.CommandType = CommandType.Text; sqlcom.CommandText = sqlStr; SqlDataAdapter sqlda = new SqlDataAdapter(sqlStr, sqlcon); DataSet ds = new DataSet(); sqlda.Fill(ds); return ds; } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } } } }

三. 实现基本功能 登录代码: 登录代码:
private void QQ_Login_Load(object sender, EventArgs e) { textBox1.UseSystemPasswordChar = true; DataSet da = MyQQ.Data.ExecuteDataSet("select xm from UserLog"); comboBox1.Items.Clear(); for (int i = 0; i < da.Tables[0].Rows.Count; i++) comboBox1.Items.Add(da.Tables[0].Rows[i][0]); }

查询好友连接数据库代码: 查询好友连接数据库代码:

private void Student_Load(object sender, EventArgs e) { DataSet da = MyQQ.Data.ExecuteDataSet("select xm from UserLog"); listBox1.Items.Clear(); for (int i = 0; i < da.Tables[0].Rows.Count; i++) listBox1.Items.Add(da.Tables[0].Rows[i][0]); }

删除用户代码实现: 删除用户代码实现
for (int i = 0; i < 好友.CheckedItems.Count; i++) { string temp = (string)好友.CheckedItems[i]; string st = "delete message_log where fz='" + temp + "' or sz='" + temp + "'"; database.liaotianDB.ExecuteSql(st); string st1 = "delete user_log where xm='" + temp + "'"; database.liaotianDB.ExecuteSql(st1); MessageBox.Show("删除成功!"); this.Hide(); new 聊天界面(name).Show(); }

注册代码实现(需要更新数据库 : 注册代码实现 需要更新数据库): 需要更新数据库
private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "") { if (textBox2.Text != "") { if (textBox2.Text == textBox3.Text) { string a = textBox1.Text; string b = textBox2.Text; float sq = 0; string sj; DateTime dt = DateTime.Now; sj = string.Format("{0}{1:d2}{2:d2} {3}", dt.Year, dt.Month, dt.Day, dt.ToLongTimeString()); string st = "insert into UserLog (xm,pw,sq,sj) values ('" + a + "','" + b + "','" + sq + "','" + sj + "')"; MyQQ.Data.ExecuteSql(st); new Done().Show(); this.Hide();

} else

{ MessageBox.Show("您输入的两次密码不相同,请重新输入"); textBox2.Text = ""; textBox3.Text = ""; this.textBox2.Focus(); } } else { MessageBox.Show("请您输入密码!"); this.textBox2.Focus(); } } else { MessageBox.Show("用户名不允许为空!"); this.textBox1.Focus(); } }

取回密码代码
private void button1_Click(object sender, EventArgs e) { if (textBox4.Text.Equals(str)) { string xm1 = textBox1.Text; string sql = "select pw from UserLog where xm='" + xm1 + "'"; DataSet da = MyQQ.Data.ExecuteDataSet(sql); if (da.Tables[0].Rows.Count == 0) { MessageBox.Show("用户不存在!"); } else if (textBox3.Text != "计算机科学与技术") { MessageBox.Show("密保答案不正确,请重新输入!"); textBox3.Focus(); } else { string pw = (string)da.Tables[0].Rows[0][0]; MessageBox.Show("你的密码是:" + pw + " 请你记住你的密码!"); } }

else { MessageBox.Show("验证码错误,请重新输入!"); Random rd = new Random(); str = ""; for (int i = 0; i < 6; i++) { str += chr[rd.Next(chr.Length - 1)]; } label6.Text = str; } }

实现聊天代码
namespace MyQQ { public partial class Chat : Form { string name;//登录人账号 string time;//读取信息时间 string sq1;//登录人权限 public Chat(string st) { InitializeComponent(); this.name = st; label2.Text = st; DataSet da = MyQQ.Data.ExecuteDataSet("select sq,sj from UserLog where xm='" + st + "'"); this.sq1 = (string)da.Tables[0].Rows[0][0]; this.time = (string)da.Tables[0].Rows[0][1]; if (this.sq1 == "0") { this.button3.Enabled = false; } else this.button3.Enabled = true; } private string getTime() { DateTime dt = DateTime.Now; if (dt.Hour > 10) { return string.Format("{0}{1:d2}{2:d2}-{3}", dt.Year, dt.Month, dt.Day, dt.ToLongTimeString()); } else { return string.Format("{0}{1:d2}{2:d2}-0{3}", dt.Year, dt.Month, dt.Day,

dt.ToLongTimeString()); } } private void button1_Click(object sender, EventArgs e) { DataSet da = MyQQ.Data.ExecuteDataSet("select * from MessageLog where sj> '" + time + "' and ( fz='" + name + "' or sz='ALL' or sz='" + name + "')"); time = getTime(); string st = "update UserLog set sj='" + time + "' where xm='" + name + "'"; MyQQ.Data.ExecuteSql(st); for (int i = 0; i < da.Tables[0].Rows.Count; i++) { listBox1.Items.Add(""); listBox1.Items.Add(""); DataSet da1 = MyQQ.Data.ExecuteDataSet("select xm from UserLog where xm='" + da.Tables[0].Rows[i][0] + "'"); if ((string)da.Tables[0].Rows[i][1] != "ALL") { DataSet da2 = MyQQ.Data.ExecuteDataSet("select xm from UserLog where xm='" + da.Tables[0].Rows[i][1] + "'"); listBox1.Items.Add(string.Format("---==={0}({1})-->{2}({3}) {4}===---", da1.Tables[0].Rows[0][0], da.Tables[0].Rows[i][0], da2.Tables[0].Rows[0][0], da.Tables[0].Rows[i][1], da.Tables[0].Rows[i][2])); } else { listBox1.Items.Add(string.Format("---==={0}({1})-->ALL da1.Tables[0].Rows[0][0], da.Tables[0].Rows[i][0], da.Tables[0].Rows[i][2])); } listBox1.Items.Add(string.Format("{0}", da.Tables[0].Rows[i][3])); } } private void button2_Click(object sender, EventArgs e) { if (checkedListBox1.Enabled == false) { //群聊 if (textBox1.Text == "") { MessageBox.Show("发送信息不允许为空!"); } {2}===---",

else { string st = "insert into MessageLog (fz,sz,sj,xx) values ('" + name + "','ALL','" + getTime() + "','" + textBox1.Text + "')"; MyQQ.Data.ExecuteSql(st); textBox1.Text = ""; } } else if (checkedListBox1.CheckedItems.Count != 0) { //私聊 if (textBox1.Text == "") { MessageBox.Show("发送信息不允许为空!"); } else { for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { string temp = (string)checkedListBox1.CheckedItems[i]; string st = "insert into MessageLog (fz,sz,sj,xx) values ('" + name + "','" + temp + "','" + getTime() + "','" + textBox1.Text + "')"; MyQQ.Data.ExecuteSql(st); } textBox1.Text = ""; } } else MessageBox.Show("请选择聊天的好友!"); } private void button3_Click(object sender, EventArgs e) { for (int i = 0; i < checkedListBox1.CheckedItems.Count; i++) { string temp = (string)checkedListBox1.CheckedItems[i]; string st = "delete MessageLog where fz='" + temp + "' or sz='" + temp + "'"; MyQQ.Data.ExecuteSql(st); string st1 = "delete UserLog where xm='" + temp + "'"; MyQQ.Data.ExecuteSql(st1); MessageBox.Show("删除成功!"); this.Hide(); new Chat(name).Show();

} } private void button4_Click(object sender, EventArgs e) { DataSet da0 = MyQQ.Data.ExecuteDataSet("select xm,pw from UserLog"); checkedListBox1.Items.Clear(); for (int i = 0; i < da0.Tables[0].Rows.Count; i++) { checkedListBox1.Items.Add(da0.Tables[0].Rows[i][0] + " (" + da0.Tables[0].Rows[i][1] + ")"); } DataSet da = MyQQ.Data.ExecuteDataSet("select * from MessageLog where sj> '" + time + "' and ( fz='" + name + "' or sz='ALL' or sz='" + name + "')"); time = getTime(); string st = "update UserLog set sj='" + time + "' where xm='" + name + "'"; MyQQ.Data.ExecuteSql(st); for (int i = 0; i < da.Tables[0].Rows.Count; i++) { listBox1.Items.Add(""); listBox1.Items.Add(""); DataSet da1 = MyQQ.Data.ExecuteDataSet("select xm from UserLog where xm='" + da.Tables[0].Rows[i][0] + "'"); if ((string)da.Tables[0].Rows[i][1] != "ALL") { DataSet da2 = MyQQ.Data.ExecuteDataSet("select xm from UserLog where xm='" + da.Tables[0].Rows[i][1] + "'"); listBox1.Items.Add(string.Format("---==={0}({1})-->{2}({3}) {4}===---", da1.Tables[0].Rows[0][0], da.Tables[0].Rows[i][0], da2.Tables[0].Rows[0][0], da.Tables[0].Rows[i][1], da.Tables[0].Rows[i][2])); } else { listBox1.Items.Add(string.Format("---==={0}({1})-->ALL da1.Tables[0].Rows[0][0], da.Tables[0].Rows[i][0], da.Tables[0].Rows[i][2])); } listBox1.Items.Add(string.Format("{0}", da.Tables[0].Rows[i][3])); } } private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { {2}===---",

new Student().Show(); } private void checkBox1_CheckedChanged(object sender, EventArgs e) { this.checkedListBox1.Enabled = !checkBox1.Checked; } private void Chat_Load(object sender, EventArgs e) { DataSet da = MyQQ.Data.ExecuteDataSet("select xm from UserLog"); checkedListBox1.Items.Clear(); for (int i = 0; i < da.Tables[0].Rows.Count; i++) checkedListBox1.Items.Add(da.Tables[0].Rows[i][0]); } private void Chat_FormClosing(object sender, FormClosingEventArgs e) { Application.Exit(); } } }

四. 小结 本聊天程序系统能实现用户注册, (如果已经注册)可以找回密 码,以及登录。 在登陆界面,点击设置,可以查看好友信息、实现修改密码 单击聊天,可进入聊天程序

在聊天过程中,可以查看聊天记录,可以给个人或者 全体发送消息。如果消息过多可以实现清屏。对于好友管理,可 以实现删除好友操作。


搜索更多“MyQQ聊天程序设计”

网站地图

All rights reserved Powered by 伤城文章网 5xts.com

copyright ©right 2010-2021。
伤城文章网内容来自网络,如有侵犯请联系客服。zhit325@126.com