コード例 #1
0
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (BaoHienRepository.testDBConnection(txtIP.Text, txtPort.Text, txtNet.Text, txtDataName.Text,
                                                   txtUsername.Text, txtPass.Text))
            {
                SettingManager.UpdateRegistry(txtIP.Text, txtPort.Text, txtNet.Text, txtDataName.Text,
                                              txtUsername.Text, txtPass.Text);
                BaoHienRepository.ResetDBDataContext();
                MessageBox.Show("Cơ sở dữ liệu đã được chuyển");
                this.Hide();
                SystemUser user = Global.CurrentUser;
                if (user != null)
                {
                    SystemUserService systemUserService = new SystemUserService();
                    user = systemUserService.GetSystemUsers().Single(u => (u.Username == Global.CurrentUser.Username) && (u.Password == Global.CurrentUser.Password));
                }
                if (user == null)
                {
                    Login frmLogin = new Login();
                    frmLogin.ShowDialog();
                }
                else
                {
                    Global.CurrentUser = user;
                }

                this.Close();
            }
            else
            {
                MessageBox.Show("Không thể kết nối cơ sở dữ liệu");
            }
        }
コード例 #2
0
        private bool UpdateAttribute(long newProductId)
        {
            DataGridViewRowCollection selectedRows            = dgvBaseAttributes.Rows;
            ProductAttributeService   productAttributeService = new ProductAttributeService();

            bool       result = true;
            List <int> newAttr = new List <int>();
            List <int> addAttr, removeAttr;

            foreach (DataGridViewRow dgv in selectedRows)
            {
                DataGridViewCheckBoxCell checkbox = (DataGridViewCheckBoxCell)dgv.Cells[0];
                if (checkbox.Value != null &&
                    (checkbox.Value.ToString().Equals(bool.TrueString) || checkbox.Value.ToString().Equals("1")))
                {
                    newAttr.Add(((BaseAttribute)dgv.DataBoundItem).Id);
                }
            }

            addAttr    = newAttr.Except(oldAttr).ToList();
            removeAttr = oldAttr.Except(newAttr).ToList();

            foreach (int add in addAttr)
            {
                ProductAttribute pa = new ProductAttribute()
                {
                    ProductId   = (int)newProductId,
                    AttributeId = add
                };
                result = productAttributeService.AddProductAttribute(pa);
            }

            foreach (int remove in removeAttr)
            {
                int removeId = -1;
                using (BaoHienDBDataContext context = new BaoHienDBDataContext(SettingManager.BuildStringConnection()))
                {
                    ProductAttribute pa = context.ProductAttributes
                                          .Where(p => p.AttributeId == remove && p.ProductId == (int)newProductId).SingleOrDefault();
                    if (pa != null)
                    {
                        removeId = pa.Id;
                    }
                }
                if (removeId != -1)
                {
                    result = productAttributeService.DeleteProductAttribute(removeId);
                }
            }
            BaoHienRepository.ResetDBDataContext();
            return(result);
        }