Ejemplo n.º 1
0
        public int Sua(int ma, String ten, String moTa, List <MenhDe> veTrai, MenhDe vePhai)
        {
            String queryLuat = "UPDATE tbLuat SET Ten=N'"
                               + ten + "', MoTa=N'"
                               + moTa + "', MaMenhDe="
                               + vePhai.Ma + " WHERE Ma=" + ma;
            int dem = da.Write(queryLuat);

            // Cap nhat cho ve trai
            // Buoc 1: Doc cac ve trai cu cua luat can cap nhat
            Luat r = new Luat();

            r = r.TimTheoMa(ma);
            if (r != null)
            {
                List <MenhDe> veTraiCu = r.VeTrai;

                // Buoc 2: Doi chieu ve trai cu va ve trai moi de lap cac danh sach
                List <int> dsCapNhat = new List <int>();
                List <int> dsThemMoi = new List <int>();
                List <int> dsXoa     = new List <int>();
                foreach (MenhDe mdc in veTraiCu)
                {
                    Boolean timThay = false;
                    foreach (MenhDe mdm in veTrai)
                    {
                        if (mdc.Ma == mdm.Ma)
                        {
                            dsCapNhat.Add(mdc.Ma);
                            timThay = true;
                            break;
                        }
                    }
                    if (!timThay)
                    {
                        dsXoa.Add(mdc.Ma);
                    }
                }
                foreach (MenhDe mdm in veTrai)
                {
                    Boolean timThay = false;
                    foreach (MenhDe mdc in veTraiCu)
                    {
                        if (mdm.Ma == mdc.Ma)
                        {
                            timThay = true;
                            break;
                        }
                    }
                    if (!timThay)
                    {
                        dsThemMoi.Add(mdm.Ma);
                    }
                }

                // Buoc 3: Thuc thi cac danh sach
                foreach (int maMenhDe in dsThemMoi)
                {
                    String query = "INSERT INTO tbVeTrai(MaLuat, MaMenhDe) VALUES("
                                   + ma + ", "
                                   + maMenhDe + ")";
                    da.Write(query);
                }
                foreach (int maMenhDe in dsXoa)
                {
                    String query = "DELETE tbVeTrai WHERE MaMenhDe="
                                   + maMenhDe;
                    da.Write(query);
                }
            }
            return(dem);
        }