示例#1
0
        private void btnChange_Click(object sender, EventArgs e)
        {
            using (LanChatDBDataContext db = new LanChatDBDataContext())
            {
                db.DeferredLoadingEnabled = false;//
                string id   = dbDTGV.SelectedCells[0].OwningRow.Cells["ID"].Value.ToString();
                string name = dbDTGV.SelectedCells[0].OwningRow.Cells["UserName"].Value.ToString();

                if (DuplicateNameCheck(db, name))
                {
                    USER change = db.USERs.Where(p => p.ID.Equals(id)).SingleOrDefault();
                    //change.ID = id; khong the thay doi id vi id la khoa chinh
                    //khong can check id trung
                    change.UserName = name;
                    //change.HashPassword = pwd;
                    db.SubmitChanges();
                    MessageBox.Show("Success!");
                }
                else
                {
                    MessageBox.Show("Fail!");
                }
            }
            btnRefresh_Click(sender, e);//refresh
        }
示例#2
0
 private void btnRefresh_Click(object sender, EventArgs e)
 {
     using (LanChatDBDataContext db = new LanChatDBDataContext())
     {
         db.DeferredLoadingEnabled = false;//
         dbDTGV.DataSource         = db.USERs.Select(p => p);
     }
 }
示例#3
0
 private void btnRefresh_Click(object sender, EventArgs e)
 {
     using (LanChatDBDataContext db = new LanChatDBDataContext())
     {
         db.DeferredLoadingEnabled = false;
         dtgvMessage.DataSource    = db.mMESSAGEs.Select(p => p);
     }
 }
示例#4
0
 public ViewDB()
 {
     InitializeComponent();
     using (LanChatDBDataContext db = new LanChatDBDataContext())
     {
         db.DeferredLoadingEnabled = false;//
         dbDTGV.DataSource         = db.USERs.Select(p => p);
     }
 }
示例#5
0
 public Messages_Database()
 {
     InitializeComponent();
     using (LanChatDBDataContext db = new LanChatDBDataContext())
     {
         db.DeferredLoadingEnabled = false;
         dtgvMessage.DataSource    = db.mMESSAGEs.Select(p => p);
     }
 }
示例#6
0
        public bool DuplicateIDCheck(LanChatDBDataContext db, string id)
        {
            bool notduplicate;
            var  q = db.USERs.Where(p => p.ID.Equals(id));

            if (q.Any())
            {
                notduplicate = false;
            }
            else
            {
                notduplicate = true;
            }
            return(notduplicate);
        }
示例#7
0
        public bool DuplicateNameCheck(LanChatDBDataContext db, string name)
        {
            bool notduplicate;
            var  q = db.USERs.Where(p => p.UserName.Equals(name));

            if (q.Any())
            {
                notduplicate = false;
            }
            else
            {
                notduplicate = true;
            }
            return(notduplicate);
        }
示例#8
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (idlength == (tbaID.Text).Length)
            {
                using (LanChatDBDataContext db = new LanChatDBDataContext())
                {
                    db.DeferredLoadingEnabled = false;//
                    string id   = tbaID.Text;
                    string name = tbaName.Text;
                    if (DuplicateIDCheck(db, id) && DuplicateNameCheck(db, name))
                    {
                        MD5    mh         = MD5.Create();
                        byte[] inputBytes = System.Text.Encoding.ASCII.GetBytes(tbaPwd.Text);//plaintext to bytes

                        byte[] hash = mh.ComputeHash(inputBytes);

                        StringBuilder sb = new StringBuilder();

                        for (int i = 0; i < hash.Length; i++)
                        {
                            sb.Append(hash[i].ToString("X2"));//X2 in hoa, x2 in thuong
                        }

                        USER add = new USER();

                        add.ID           = tbaID.Text;
                        add.UserName     = tbaName.Text;
                        add.HashPassword = sb.ToString();

                        db.USERs.InsertOnSubmit(add);
                        db.SubmitChanges();
                        MessageBox.Show("Success!");
                    }
                    else
                    {
                        MessageBox.Show("Duplicated data!");
                    }
                    btnRefresh_Click(sender, e);//refresh
                    tbaID.Text   = "";
                    tbaName.Text = "";
                    tbaPwd.Text  = "";
                }
            }
            else
            {
                MessageBox.Show("Id's length must equal 5!!!");
            }
        }
示例#9
0
        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Delete selected data?!", "Caution!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);

            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                using (LanChatDBDataContext db = new LanChatDBDataContext())
                {
                    db.DeferredLoadingEnabled = false;//
                    string ID     = dbDTGV.SelectedCells[0].OwningRow.Cells["ID"].Value.ToString();
                    USER   delete = db.USERs.Where(p => p.ID.Equals(ID)).SingleOrDefault();
                    db.USERs.DeleteOnSubmit(delete);
                    db.SubmitChanges();
                }
                btnRefresh_Click(sender, e);//refresh
            }
        }
示例#10
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            using (LanChatDBDataContext db = new LanChatDBDataContext())
            {
                db.DeferredLoadingEnabled = false;//

                if (rbtnName.Checked == true)
                {
                    dbDTGV.DataSource = db.USERs.Where(p => p.UserName.Equals(tbName.Text));
                }

                if (rbtnID.Checked == true)
                {
                    dbDTGV.DataSource = db.USERs.Where(p => p.ID.Equals(tbID.Text));
                }
            }
            tbID.Text   = "";
            tbName.Text = "";
        }
示例#11
0
        private void btnSearch_Click(object sender, EventArgs e)
        {
            using (LanChatDBDataContext db = new LanChatDBDataContext())
            {
                db.DeferredLoadingEnabled = false;//

                if (rbtnSender.Checked == true)
                {
                    dtgvMessage.DataSource = db.mMESSAGEs.Where(p => p.IDSender.Equals(tbSender.Text));
                }

                if (rbtnReciever.Checked == true)
                {
                    dtgvMessage.DataSource = db.mMESSAGEs.Where(p => p.IDReciever.Equals(tbReciever.Text));
                }
            }
            tbSender.Text   = "";
            tbReciever.Text = "";
        }
示例#12
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            if (txtInput.Text != string.Empty)
            {
                BroadcastData("Message|Admin|" + txtInput.Text);
                txtReceive.Text += "Admin: " + txtInput.Text + "\r\n";
                //***save message
                using (LanChatDBDataContext db = new LanChatDBDataContext())
                {
                    db.DeferredLoadingEnabled = false;

                    mMESSAGE mes = new mMESSAGE();

                    var conversation = from c in db.mMESSAGEs
                                       where c.IDSender == "SERVE" && c.IDReciever == "PUBLI"
                                       select c;
                    if (conversation.Any())//co ton tai roi
                    {
                        mMESSAGE nmes = conversation.First();
                        nmes.Content += "Admin:" + txtInput.Text + "|";

                        db.SubmitChanges();
                    }
                    else//chua ton tai
                    {
                        mes.IDSender   = "SERVE";
                        mes.IDReciever = "PUBLI";
                        mes.Content    = "Admin:" + txtInput.Text + "|";
                        db.mMESSAGEs.InsertOnSubmit(mes);
                        db.SubmitChanges();
                    }
                }
                //save message***
                txtInput.Text = "";
            }
        }
示例#13
0
        private void client_Received(Client sender, byte[] data)
        {
            this.Invoke(() =>
            {
                for (int i = 0; i < clientList.Items.Count; i++)
                {
                    var client = clientList.Items[i].Tag as Client;
                    if (client == null || client.Ip != sender.Ip)
                    {
                        continue;
                    }
                    var command = Encoding.ASCII.GetString(data).Split('|');
                    switch (command[0])
                    {
                    case "Connect":
                        //txtReceive.SelectionColor = System.Drawing.Color.Red;

                        txtReceive.Text += "<< " + command[1] + " joined the room >>\r\n";

                        clientList.Items[i].SubItems[1].Text = command[1];     // nickname
                        clientList.Items[i].SubItems[2].Text = command[2];     // status
                        string users = string.Empty;
                        for (int j = 0; j < clientList.Items.Count; j++)
                        {
                            users += clientList.Items[j].SubItems[1].Text + "|";
                        }
                        BroadcastData("Users|" + users.TrimEnd('|'));
                        BroadcastData("RefreshChat|" + command[1]);    //nguoi vao sau khong thay nguoi vao truoc join room
                        break;

                    case "Message":
                        //public chat
                        txtReceive.Text += command[1] + ": " + command[2] + "\r\n";
                        BroadcastData("RefreshChat|" + command[1] + "|" + command[2]);
                        //***save message
                        using (LanChatDBDataContext db = new LanChatDBDataContext())
                        {
                            db.DeferredLoadingEnabled = false;

                            mMESSAGE mes = new mMESSAGE();

                            var id = from Id in db.USERs
                                     where Id.UserName == command[1].ToString()
                                     select Id.ID;    //lay id tu table users

                            string newid     = id.FirstOrDefault().ToString();
                            var conversation = from c in db.mMESSAGEs
                                               where c.IDSender == newid && c.IDReciever == "PUBLI"
                                               select c;
                            if (conversation.Any())    //co ton tai roi
                            {
                                mMESSAGE nmes = conversation.First();
                                nmes.Content += command[1] + ": " + command[2] + "|";

                                db.SubmitChanges();
                            }
                            else    //chua ton tai
                            {
                                mes.IDSender   = newid;
                                mes.IDReciever = "PUBLI";
                                mes.Content    = command[1] + ": " + command[2] + "|";
                                db.mMESSAGEs.InsertOnSubmit(mes);
                                db.SubmitChanges();
                            }
                        }
                        //save message***
                        break;

                    case "pMessage":
                        //private chat client voi server
                        this.Invoke(() =>
                        {
                            pChat.txtReceive.Text += command[1] + ": " + command[2] + "\r\n";
                            //***save message
                            using (LanChatDBDataContext db = new LanChatDBDataContext())
                            {
                                db.DeferredLoadingEnabled = false;

                                mMESSAGE mes = new mMESSAGE();

                                var id = from Id in db.USERs
                                         where Id.UserName == command[1].ToString()
                                         select Id.ID;    //lay id tu table users

                                string newid     = id.FirstOrDefault().ToString();
                                var conversation = from c in db.mMESSAGEs
                                                   where c.IDSender == newid && c.IDReciever == "SERVE"
                                                   select c;
                                if (conversation.Any())    //co ton tai roi
                                {
                                    mMESSAGE nmes = conversation.First();
                                    nmes.Content += command[1] + ": " + command[2] + "|";

                                    db.SubmitChanges();
                                }
                                else    //chua ton tai
                                {
                                    mes.IDSender   = newid;
                                    mes.IDReciever = "SERVE";
                                    mes.Content    = command[1] + ": " + command[2] + "|";
                                    db.mMESSAGEs.InsertOnSubmit(mes);
                                    db.SubmitChanges();
                                }
                            }
                        });
                        break;

                    case "pChat":

                        break;

                    case "pChatwithUser":
                        //private chat client voi client
                        this.Invoke(() =>
                        {
                            if (command[3] != "Server")
                            {
                                txtReceive.Text += command[1] + " to " + command[3] + ": " + command[2] + "\r\n";
                                string onlychat  = Encoding.ASCII.GetString(data);
                                BroadcastData(onlychat);
                                //***save message
                                using (LanChatDBDataContext db = new LanChatDBDataContext())
                                {
                                    db.DeferredLoadingEnabled = false;

                                    mMESSAGE mes = new mMESSAGE();

                                    var id1 = from Id in db.USERs
                                              where Id.UserName == command[1].ToString()
                                              select Id.ID;   //lay id tu table users

                                    var id2 = from Id in db.USERs
                                              where Id.UserName == command[3].ToString()
                                              select Id.ID;    //lay id tu table users


                                    string newid     = id1.FirstOrDefault().ToString();
                                    string newid2    = id2.FirstOrDefault().ToString();
                                    var conversation = from c in db.mMESSAGEs
                                                       where c.IDSender == newid && c.IDReciever == newid2
                                                       select c;
                                    if (conversation.Any())    //co ton tai roi
                                    {
                                        mMESSAGE nmes = conversation.First();
                                        nmes.Content += command[1] + ": " + command[2] + "|";

                                        db.SubmitChanges();
                                    }
                                    else    //chua ton tai
                                    {
                                        mes.IDSender   = newid;
                                        mes.IDReciever = newid2;
                                        mes.Content    = command[1] + ": " + command[2] + "|";
                                        db.mMESSAGEs.InsertOnSubmit(mes);
                                        db.SubmitChanges();
                                    }
                                }
                            }
                        });
                        break;
                    }
                }
            });
        }