private void ShowClientInfo() { DBDeal dbDeal = new DBDeal(); string selectstr = "select * from IDInfo"; dbDeal.SqlFullDataGridView(dataGridView1, selectstr); }
//修改该账号的登陆状态到数据库 客户端信息 登陆状态 loginOrclose 1 代表登陆 0代表下线 private void UpdateLoginStateAndTime_Close(List <Object> info, int isloginState, int loginOrclose) { string updateStr; if (loginOrclose == 1) { updateStr = String.Format("update IDInfo set lastlogindate ='{0}',islogin={1} where ip='{2}' and port = {3} and pwd='{4}' ", DateTime.Now.ToString(), isloginState, info[1].ToString(), Convert.ToInt32(info[2].ToString()), info[3], ToString()); } else { if (info[1] == null) //如果是在主窗体之外退出的 则info只有一条数据 其他的都为空 { return; } updateStr = String.Format("update IDInfo set islogin={0} where ip='{1}' and port = {2}", 0, info[1].ToString(), Convert.ToInt32(info[2].ToString()) ); } DBDeal dbDeal = new DBDeal(); string tips = (loginOrclose == 1) ? "上线" : "下线"; // 提示语句 int i = dbDeal.SqlAction(updateStr); if (i > 0) { OperateAboutUIControl_RichTextBox(richTextBox1, info[1].ToString() + tips); } }
//注册的时候 对比信息 是否已有账号 已经存在就返回true 不存在返回false private bool IsExistID(List <Object> info) { string selectstr = String.Format("select * from IDInfo where ip='{0}' and port = {1}", info[1].ToString(), Convert.ToInt32(info[2].ToString())); DBDeal dbDeal2 = new DBDeal(); return(dbDeal2.SqlSelectHas(selectstr)); //已经存在账号则返回客户端 }
//将所有的id信息放到infolist 返回到客户端的信息 private List <Object> SaveClientInfoToList(List <Object> infolist) { string selectStr = "select name,ip,port,islogin from IDInfo"; DBDeal dbDeal = new DBDeal(); SqlDataReader sdr = dbDeal.SqlGetThreeInfo(selectStr); while (sdr.Read()) { infolist.Add(sdr[0].ToString()); //name infolist.Add(sdr[1].ToString()); //ip infolist.Add(sdr[2].ToString()); //port infolist.Add(sdr[3].ToString()); //islogin } dbDeal.ShutDown(); return(infolist); }
void DeleteClientInfo() { string ip = dataGridView1.CurrentRow.Cells[1].Value.ToString(); int port = Convert.ToInt32(dataGridView1.CurrentRow.Cells[2].Value.ToString()); string pwd = dataGridView1.CurrentRow.Cells[3].Value.ToString(); dataGridView1.CurrentRow.Cells[0].Value.ToString(); string deleteStr = String.Format("delete from IDInfo where ip='{0}' and port = {1} and pwd='{2}' ", ip, port, pwd); DBDeal dbDeal = new DBDeal(); int i = dbDeal.SqlAction(deleteStr); if (i > 0) { OperateAboutUIControl_RichTextBox(richTextBox1, ip + "\n" + port + "\n" + pwd + "\n已删除!!!"); } }
//根据ip port获取信息 (22) private List <Object> GetUserInfo(List <Object> temp, List <Object> infolist) { string selectStr = String.Format("select * from IDInfo where ip='{0}' and port='{1}'", temp[0].ToString(), temp[1].ToString()); DBDeal dbDeal = new DBDeal(); SqlDataReader sdr = dbDeal.SqlGetThreeInfo(selectStr); sdr.Read(); infolist.Add(sdr[0].ToString()); //id infolist.Add(sdr[1].ToString()); //ip infolist.Add(sdr[2].ToString()); //port //infolist.Add(sdr[3].ToString());//pwd infolist.Add(sdr[4].ToString()); //name infolist.Add(sdr[5].ToString()); //sex infolist.Add(sdr[6].ToString()); //lastlogindate infolist.Add(sdr[7].ToString()); //islogin infolist.Add(sdr[8].ToString()); //other dbDeal.ShutDown(); return(infolist); }
//------------------------------------------------------------------------------------------ //判断是登陆还是注册 用户列表 退出 用户信息 private string IsLoginOrRegister(List <Object> info) { ShowInfoOnView(info, info[0].ToString()); switch (info[0].ToString()) { case "1": //是注册的话先判断是否存在 不存在则新建并进行数据存储 if (IsExistID(info) == false) { string insertStr = String.Format("insert into IDInfo(ip,port,pwd,name,sex,lastlogindate,islogin) values('{0}',{1},'{2}','{3}','{4}','{5}',{6})", info[1].ToString(), Convert.ToInt32(info[2].ToString()), info[3].ToString(), info[4].ToString(), info[5].ToString(), DateTime.Now.ToString(), 0); //0 代表下线 1 代表上线 DBDeal dbDeal = new DBDeal(); int i = dbDeal.SqlAction(insertStr); if (i > 0) { //更新服务器的显示信息 dataGridView1.Invoke((MethodInvoker)(() => { ShowClientInfo(); })); //在注册时候会出现错误 会直接进入下一线程 交给ui线程 在等ui线程?? 都在等(死锁) //MessageBox.Show("注册成功!!!", "提示", // MessageBoxButtons.OK, // MessageBoxIcon.Information); } return("2"); } else { return("23"); } case "3": //是登陆的话进行数据对比 string selectstr = String.Format("select * from IDInfo where ip='{0}' and port = {1} and pwd='{2}'", info[1].ToString(), Convert.ToInt32(info[2].ToString()), info[3], ToString()); DBDeal dbDeal2 = new DBDeal(); if (dbDeal2.SqlSelectHas(selectstr)) //有账号则返回客户端 "4" { UpdateLoginStateAndTime_Close(info, 1, 1); //修改该账号的登陆状态到数据库 dataGridView1.Invoke((MethodInvoker)(() => //更新服务器的显示信息 { ShowClientInfo(); })); return("4"); } return("gg"); //无账号则返回客户端 "gg" case "7": //进行客户端好友列表更新 return("7"); case "12": //代表下线 //是退出的话进行数据更新 UpdateLoginStateAndTime_Close(info, 1, 0); //修改该账号的登陆状态到数据库 dataGridView1.Invoke((MethodInvoker)(() => //更新服务器的显示信息 { ShowClientInfo(); })); return("12"); case "22": //代表给客户端发送ip port对应的用户信息 //代表给客户端发送ip port对应的用户信息 temp = new List <object>(); temp.Add(info[1].ToString()); temp.Add(info[2].ToString()); return("22"); default: return("0"); //代表不存在 } }