severdata.cs
资源名称:类似QQ聊天程序.rar [点击查看]
上传用户:yan_wy
上传日期:2007-06-09
资源大小:112k
文件大小:13k
源码类别:
ICQ/即时通讯
开发平台:
C#
- using System;
- using System.Data;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- using System.Net;
- namespace qq
- {
- /// <summary>
- /// severdata 的摘要说明。
- /// </summary>
- public class severdata : System.Windows.Forms.Form
- {
- public DataSet dataset;
- Form1 form;
- private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
- private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
- public System.Data.SqlClient.SqlConnection sqlConnection1;
- public System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
- private System.Data.SqlClient.SqlCommand sqlSelectCommand2;
- private System.Data.SqlClient.SqlCommand sqlInsertCommand2;
- private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter2;
- /// <summary>
- /// 必需的设计器变量。
- /// </summary>
- private System.ComponentModel.Container components = null;
- public severdata(Form1 sform)
- {
- //
- // Windows 窗体设计器支持所必需的
- //
- form=sform;
- dataset=new DataSet();
- dataset.Tables.Add("user");
- dataset.Tables.Add("me");
- dataset.Tables.Add("you");
- InitializeComponent();
- //
- // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
- //
- }
- /// <summary>
- /// 清理所有正在使用的资源。
- /// </summary>
- protected override void Dispose( bool disposing )
- {
- if( disposing )
- {
- if(components != null)
- {
- components.Dispose();
- }
- }
- base.Dispose( disposing );
- }
- #region Windows 窗体设计器生成的代码
- /// <summary>
- /// 设计器支持所需的方法 - 不要使用代码编辑器修改
- /// 此方法的内容。
- /// </summary>
- private void InitializeComponent()
- {
- this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
- this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
- this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
- this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
- this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
- this.sqlInsertCommand2 = new System.Data.SqlClient.SqlCommand();
- this.sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
- //
- // sqlSelectCommand1
- //
- this.sqlSelectCommand1.CommandText = "SELECT id, mima, name, sex, picture, age, jianjie, state, ip FROM [user]";
- this.sqlSelectCommand1.Connection = this.sqlConnection1;
- //
- // sqlInsertCommand1
- //
- this.sqlInsertCommand1.CommandText = "INSERT INTO [user] (id, mima, name, sex, picture, age, jianjie, state, ip) VALUES" +
- " (@id, @mima, @name, @sex, @picture, @age, @jianjie, @state, @ip); SELECT id, mi" +
- "ma, name, sex, picture, age, jianjie, state, ip FROM [user]";
- this.sqlInsertCommand1.Connection = this.sqlConnection1;
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@id", System.Data.SqlDbType.Int, 4, "id"));
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@mima", System.Data.SqlDbType.NVarChar, 10, "mima"));
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 10, "name"));
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@sex", System.Data.SqlDbType.VarChar, 10, "sex"));
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@picture", System.Data.SqlDbType.VarChar, 10, "picture"));
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@age", System.Data.SqlDbType.Int, 4, "age"));
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@jianjie", System.Data.SqlDbType.VarChar, 2147483647, "jianjie"));
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@state", System.Data.SqlDbType.Int, 4, "state"));
- this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ip", System.Data.SqlDbType.VarChar, 10, "ip"));
- //
- // sqlDataAdapter1
- //
- this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
- this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
- this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
- new System.Data.Common.DataTableMapping("Table", "user", new System.Data.Common.DataColumnMapping[] {
- new System.Data.Common.DataColumnMapping("id", "id"),
- new System.Data.Common.DataColumnMapping("mima", "mima"),
- new System.Data.Common.DataColumnMapping("name", "name"),
- new System.Data.Common.DataColumnMapping("sex", "sex"),
- new System.Data.Common.DataColumnMapping("picture", "picture"),
- new System.Data.Common.DataColumnMapping("age", "age"),
- new System.Data.Common.DataColumnMapping("jianjie", "jianjie"),
- new System.Data.Common.DataColumnMapping("state", "state"),
- new System.Data.Common.DataColumnMapping("ip", "ip")})});
- //
- // sqlSelectCommand2
- //
- this.sqlSelectCommand2.CommandText = "SELECT uid, friendid FROM friends";
- this.sqlSelectCommand2.Connection = this.sqlConnection1;
- //
- // sqlInsertCommand2
- //
- this.sqlInsertCommand2.CommandText = "INSERT INTO friends(uid, friendid) VALUES (@uid, @friendid); SELECT uid, friendid" +
- " FROM friends";
- this.sqlInsertCommand2.Connection = this.sqlConnection1;
- this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@uid", System.Data.SqlDbType.Int, 4, "uid"));
- this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@friendid", System.Data.SqlDbType.Int, 4, "friendid"));
- //
- // sqlDataAdapter2
- //
- this.sqlDataAdapter2.InsertCommand = this.sqlInsertCommand2;
- this.sqlDataAdapter2.SelectCommand = this.sqlSelectCommand2;
- this.sqlDataAdapter2.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
- new System.Data.Common.DataTableMapping("Table", "friends", new System.Data.Common.DataColumnMapping[] {
- new System.Data.Common.DataColumnMapping("uid", "uid"),
- new System.Data.Common.DataColumnMapping("friendid", "friendid")})});
- //
- // severdata
- //
- this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
- this.ClientSize = new System.Drawing.Size(292, 273);
- this.Name = "severdata";
- this.Text = "severdata";
- this.Closing += new System.ComponentModel.CancelEventHandler(this.severdata_Closing);
- this.Closed += new System.EventHandler(this.severdata_Closed);
- this.Paint += new System.Windows.Forms.PaintEventHandler(this.severdata_Paint);
- }
- #endregion
- public int yanzhen()
- {
- try
- {
- //下面是用户登陆时,验证密码是否错误,如果错误,则抛出错误,同时返回错误信息
- System.Data.SqlClient.SqlCommand sss=new System.Data.SqlClient.SqlCommand();
- sss.CommandText="SELECT name,mima,sex,picture,age,jianjie from [user] where id="+form.userid;
- sss.Connection=this.sqlConnection1;
- this.sqlDataAdapter1.SelectCommand=sss;
- this.sqlDataAdapter1.Fill(dataset,"me");
- if(this.dataset.Tables["me"].Rows.Count==0)
- return 0;//没有此用户
- foreach(DataRow dt in this.dataset.Tables["me"].Rows)
- {
- if(form.mima!=(dt[1].ToString()).Trim())
- return 0;//用户密码错误
- form.name=dt[0].ToString();
- }
- return 1;//密码正缺
- }
- catch(Exception ee)
- {
- return 2;//异常处理
- }
- }
- public void close()
- {
- //下面是当用户下线时,更新数据库,把它的state变为0,同时发送下线通知消息,以便它的好友用户处理
- System.Data.SqlClient.SqlCommand updata=new System.Data.SqlClient.SqlCommand();
- updata.CommandText= "UPDATE [user] SET state=0 WHERE id="+form.userid;
- updata.Connection=this.sqlConnection1;
- this.sqlConnection1.Open();
- updata.ExecuteNonQuery();//执行语句
- this.sqlConnection1.Close();
- foreach(UserControl1 dt in form.userlist)
- {
- if(dt.state=="1")
- {
- form.use.sendmessage(form.userid);
- form.use.sendmessage(dt.uid);
- form.use.sendmessage("下线通知");//下线通知
- }
- }
- }
- public void addfriends()
- {
- }
- public void adduse(string name,string mima,string jianjie,string picture)
- {
- System.Data.SqlClient.SqlCommand select=new System.Data.SqlClient.SqlCommand();
- select.CommandText="SELECT top 1 id from [user] order by id desc";
- select.Connection=this.sqlConnection1;
- //这里是添加好友,上面先找到QQ号码最大的一个,这样在注册号码时,就自动分配给它的QQ号是当前的最大号加1
- try
- {
- this.sqlConnection1.Open();
- string a= (select.ExecuteScalar()).ToString();
- int b=int.Parse(a);//字符转换为数据
- b=b+1;//号码加1
- a=b.ToString();
- this.form.userid=a;
- string hostname=Dns.GetHostName();
- IPAddress ipAddress = Dns.GetHostByName(hostname).AddressList[0];
- //得到主机ip
- //下面是把用户加到数据库,insert语句的格式是insert into (×× )value(对应的数据);
- System.Data.SqlClient.SqlCommand insert=new System.Data.SqlClient.SqlCommand();
- insert.CommandText="INSERT INTO [user] (id, mima, name, sex, picture, age, jianjie, state, ip) VALUES ("+//+b+","+form.mima+","+name+",男,1,20,"+jianjie+",0,0)";
- a+",'"+mima+"','"+name+"','男','"+picture+"',20,'"+jianjie+"',0,'"+ipAddress.ToString()+"')";
- MessageBox.Show("你的QQ号:"+b);//向用户说明你的QQ号
- insert.Connection=this.sqlConnection1;
- insert.ExecuteNonQuery();//执行查询
- insert.Connection=this.sqlConnection1;
- this.sqlConnection1.Close();
- }
- catch(Exception ee)
- {
- MessageBox.Show(ee.ToString());// 出错处理
- }
- }
- public void shanxian()
- {
- foreach(UserControl1 dt in form.userlist)
- {
- if(dt.state=="1")
- {
- form.use.sendmessage(form.userid);
- form.use.sendmessage(dt.uid);
- form.use.sendmessage("上线通知");
- }
- }
- }
- public void Startset()
- {
- try
- {
- string hostname=Dns.GetHostName();
- IPAddress ipAddress = Dns.GetHostByName(hostname).AddressList[0];
- //获得主机ip,用DNS查询
- System.Data.SqlClient.SqlCommand updata=new System.Data.SqlClient.SqlCommand();
- updata.CommandText= "UPDATE [user] SET state=1 ,ip='"+ipAddress.ToString()+"' WHERE id="+form.userid;
- /*
- 这句话是把数据库中的state由0变为1,同时储存它的ip,意思是用户上线了。这里的查询的号码为form。userid的用户
- 这里我要提醒一条的是,在写数据库语句时,如果是字符串,要加单引号,如果是数字,就不用了
- */
- updata.Connection=this.sqlConnection1;
- this.sqlConnection1.Open();//打开连接
- updata.ExecuteNonQuery();//执行上面的语句,这里一定要打开数据连接
- this.sqlConnection1.Close();//关闭连接
- System.Data.SqlClient.SqlCommand select=new System.Data.SqlClient.SqlCommand();
- select.CommandText= "SELECT [user].id,[user].name,[user].sex,[user].picture,[user].age,[user].jianjie,[user].state,[user].ip FROM [user] , [friends] where [friends].uid="+form.userid+" and [user].id =[friends].friendid";
- select.Connection=this.sqlConnection1;
- this.sqlDataAdapter1.SelectCommand=select;
- this.sqlDataAdapter1.Fill(dataset,"user");
- //上面是选择语句,选择本用户的好友信息,然后保存在dataset里的user表里
- //这里的sqlDataAdapter执行时不用写sqlconnect。open因为它集成了打开操作。
- //另外在数据库的语句中,凡是数据库的表都要加『』符合。
- }
- catch(Exception ee)
- {
- this.form.isinline=0;//出错处理。表示连接不到数据库
- }
- //下面是在用户界面上显示好友,数据来自dataset的user
- foreach(DataRow dt in this.dataset.Tables["user"].Rows)
- {
- UserControl1 userControl13=new UserControl1(form);
- userControl13.Dock=DockStyle.Top;
- userControl13.uid=dt[0].ToString();
- userControl13.name=dt[1].ToString();
- userControl13.sex=dt[2].ToString();
- userControl13.picture=dt[3].ToString();
- userControl13.age=dt[4].ToString();
- userControl13.jianjie=dt[5].ToString();
- userControl13.state=dt[6].ToString();
- userControl13.ip=dt[7].ToString().Trim();
- userControl13.setname(dt[1].ToString());
- userControl13.setimage((dt[3].ToString()).Trim());
- form.userlist.Add(userControl13);
- }
- foreach(UserControl1 dt in form.userlist)
- {
- if(dt.state=="0")
- form.panel4.Controls.Add(dt);
- }
- foreach(UserControl1 dt in form.userlist)
- {
- if(dt.state=="1")
- {
- form.panel4.Controls.Add(dt);
- }
- }
- }
- private void severdata_Closed(object sender, System.EventArgs e)
- {
- }
- private void severdata_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- }
- private void severdata_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
- {
- }
- }
- }