Пример #1
0
 static void Main(string[] args)
 {
     using (DBCTxt db = new DBCTxt())
     {
         var list = db.UserInfor;
         Console.WriteLine(list.Count());
     }
     Console.WriteLine("Over");
     Console.ReadLine();
 }
Пример #2
0
        private void dgvList_RowHeaderMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            int id;

            if (int.TryParse(dgvList.Rows[e.RowIndex].Cells["ID"].Value.ToString(), out id))
            {
                if (isEFRun)
                {
                    EFClassLib.TableModel.UserInfor usInfor = null;
                    using (DBCTxt db = GetEFContext())
                    {
                        usInfor = db.UserInfor.Where(x => x.id == id).FirstOrDefault();

                        if (usInfor != null)
                        {
                            EFAddOrModifyUser(usInfor);
                        }
                        else
                        {
                            txtInfors.Text = "The User is not exists.";
                        }
                    }
                }
                else
                {
                    LDB.UserInfor usInfor = null;
                    using (var ldb = GetLinqContext())
                    {
                        usInfor = ldb.UserInfors.Where(x => x.id == id).FirstOrDefault();
                        if (usInfor != null)
                        {
                            LinqAddOrModifyUser(usInfor);
                        }
                        else
                        {
                            txtInfors.Text = "The User is not exists.";
                        }
                    }
                }
            }
        }
Пример #3
0
        private void RefreshDataByEF()
        {
            isEFRun = true;
            using (DBCTxt db = GetEFContext())
            {
                var list = (from x in db.UserInfor
                            select new
                {
                    id = x.id,
                    FirstName = x.FirstName,
                    LastName = x.LastName,
                    LoginDate = x.LoginDate,
                    Password = x.Password,
                    UserName = x.UserName
                }).ToList();
                dgvList.Columns.Clear();
                dgvList.DataSource = list;
            }
            AddAndShowMessages("EF load finished");

            this.btnDelete.Text = "EF-Delete";
        }
Пример #4
0
        private void EFAddOrModifyUser(EFClassLib.TableModel.UserInfor UserInf)
        {
            isEFRun = true;
            AddUser addusFrm = new AddUser();

            addusFrm.SetEFModiInfor(UserInf);

            if (UserInf == null)
            {
                if (addusFrm.ShowDialog() == DialogResult.OK)
                {
                    var usInfor = addusFrm.GetEFUserInfor();
                    usInfor.Address = new List <EFClassLib.TableModel.Address>();
                    usInfor.Address.Add(new EFClassLib.TableModel.Address {
                        addr = "yyyy", Email = "*****@*****.**", phone = "000000"
                    });
                    usInfor.Address.Add(new EFClassLib.TableModel.Address {
                        addr = "yyyy", Email = "*****@*****.**", phone = "000000"
                    });
                    usInfor.Address.Add(new EFClassLib.TableModel.Address {
                        addr = "yyyy", Email = "*****@*****.**", phone = "000000"
                    });
                    if (usInfor != null)
                    {
                        using (DBCTxt db = GetEFContext())
                        {
                            db.UserInfor.Add(usInfor);
                            db.SaveChanges();
                        }
                        RefreshDataByEF();
                    }
                    else
                    {
                        AddAndShowMessages("Nothing is Add");
                    }
                }
            }
            else
            {
                //addusFrm.SetEFModiInfor(UserInf);
                switch (addusFrm.ShowDialog())
                {
                case DialogResult.OK:
                    var usInfor = addusFrm.GetEFUserInfor();
                    if (usInfor != null)
                    {
                        using (DBCTxt db = GetEFContext())
                        {
                            var uif = db.UserInfor.Where(x => x.id == UserInf.id).FirstOrDefault();
                            if (uif != null)
                            {
                                uif.FirstName = usInfor.FirstName;
                                uif.LastName  = usInfor.LastName;
                                uif.Password  = usInfor.Password;
                                uif.UserName  = usInfor.UserName;
                                foreach (var addr in uif.Address)
                                {
                                    addr.Email = "*****@*****.**";
                                }
                                db.SaveChanges();
                            }
                        }
                        RefreshDataByEF();
                    }
                    else
                    {
                        AddAndShowMessages("Nothing is Modify");
                    }
                    break;

                case DialogResult.No:
                    using (DBCTxt db = GetEFContext())
                    {
                        db.Database.ExecuteSqlCommand("delete from UserInfors where id=@id"
                                                      , new SqlParameter("@id", UserInf.id)
                                                      );
                    }
                    RefreshDataByEF();
                    break;

                default:
                    break;
                }
            }
        }