コード例 #1
0
        public IActionResult createUser(User newUser)
        {
            if (ModelState.IsValid)
            {
                if (dbContext.users.Any(u => u.email == newUser.email))
                {
                    return(View("Index"));
                }
                PasswordHasher <User> Hasher = new PasswordHasher <User>();
                newUser.password = Hasher.HashPassword(newUser, newUser.password);
                System.Console.WriteLine(newUser.password.ToString());
                System.Console.WriteLine("--------------------------------------------------------------------------");
                dbContext.users.Add(newUser);
                dbContext.SaveChanges();
                HttpContext.Session.SetInt32("id", newUser.userid);
                System.Console.WriteLine(newUser.userid);
                return(RedirectToAction("Account"));

                System.Console.WriteLine("*******************************************************************");
            }
            else
            {
                return(View("Index", newUser));
            }
        }
コード例 #2
0
        public IActionResult Register(Both newUser)
        {
            User user = newUser.user;

            if (ModelState.IsValid)
            {
                if (dbContext.Users.Any(u => u.Email == user.Email))
                {
                    ModelState.AddModelError("user.Email", "Email is already registered");
                    return(View("Index"));
                }
                else
                {
                    PasswordHasher <User> Hasher = new PasswordHasher <User>();
                    user.Password = Hasher.HashPassword(newUser.user, user.Password);
                    dbContext.Add(newUser.user);
                    dbContext.SaveChanges();
                    HttpContext.Session.SetInt32("UserId", user.UserId);
                    return(RedirectToAction("Success"));
                }
            }
            else
            {
                return(View("Index"));
            }
        }
コード例 #3
0
 /// <summary>
 /// 增加柜台管理员
 /// </summary>
 /// <param name="manager">Mid,Mname,MPassword</param>
 public void AddData(Managers manager)
 {
     using (var dbContext = new bankContext())
     {
         //修改数据库信息最好有一些事务操作
         using (var transaction = dbContext.Database.BeginTransaction())
         {
             try
             {
                 dbContext.Add(manager);
                 dbContext.SaveChanges();
                 transaction.Commit();
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
                 transaction.Rollback();
             }
         }
     }
 }
コード例 #4
0
 /// <summary>
 /// 绑定银行卡,数据访问层,根据储户账号Uid,修改Ucid
 /// </summary>
 /// <param name="depositor">前端传入DepositorAndCard对象,属性:Duid,Dname,Dcid</param>
 public void UpdataBandData(DepositorAndCard depositor)
 {
     using (var dbContext = new bankContext())
     {
         //修改数据库信息最好有一些事务操作
         using (var transaction = dbContext.Database.BeginTransaction())
         {
             try
             {
                 var sql = @"Update Depositors SET Ucid =  {0},Uicid={1} WHERE Uid =  {2}";
                 dbContext.Database.ExecuteSqlCommand(sql, depositor.Dcid, depositor.Dicid, depositor.Duid);
                 dbContext.SaveChanges();
                 transaction.Commit();
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
                 transaction.Rollback();
             }
         }
     }
 }
コード例 #5
0
 /// <summary>
 /// 更新银行卡表,根据cid,更新活期存款余额
 /// </summary>
 /// <param name="cid"></param>
 /// <param name="money"></param>
 public void UpdateCardsData(int cid, double money)
 {
     using (var dbContext = new bankContext())
     {
         //修改数据库信息最好有一些事务操作
         using (var transaction = dbContext.Database.BeginTransaction())
         {
             try
             {
                 var sql = @"Update Cards SET CflowBalance =  {0} WHERE Cid =  {1}";
                 dbContext.Database.ExecuteSqlCommand(sql, money, cid);
                 dbContext.SaveChanges();
                 transaction.Commit();
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
                 transaction.Rollback();
             }
         }
     }
 }
コード例 #6
0
 /// <summary>
 /// 记录表Records中增加记录,该层为访问层,传入参数:DepositorAndCard,操作类型int v,double money, int mid
 /// </summary>
 /// <param name="dAndC">传入从cooike中获取的储户和储户银行卡信息。其对象为DepositorAndCard</param>
 /// <param name="v">出入参数v代表类型,1:代表取款,2:代表活期存款,其他:代表定期存款。每次传入一个类型的值,其他两项字段默认为0</param>
 /// <param name="money">金额</param>
 /// <param name="mid">业务办理员</param>
 public void AddData(DepositorAndCard dAndC, int icid, int v, double money, int mid)
 {
     using (var dbContext = new bankContext())
     {
         //修改数据库信息最好有一些事务操作
         using (var transaction = dbContext.Database.BeginTransaction())
         {
             try
             {
                 Records records = new Records();
                 records.Rcid  = (int)dAndC.Dcid;
                 records.Ruid  = dAndC.Duid;
                 records.Rmid  = mid;
                 records.Ricid = icid;
                 if (v == 1)
                 {
                     records.Rwithdrawals = money;
                 }
                 else if (v == 2)
                 {
                     records.RflowDeposit = money;
                 }
                 else
                 {
                     records.RfixDeposit = money;
                 }
                 dbContext.Add(records);
                 dbContext.SaveChanges();
                 transaction.Commit();
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message);
                 transaction.Rollback();
             }
         }
     }
 }
コード例 #7
0
        static void Main(string[] args)
        {
            Users       user       = new Users();
            Information infomation = new Information();

            #region 操作数据库中的视图
            using (ViewContext dbContext = new ViewContext())
            {
                //通过ViewContext.Iformation属性从数据库中查询视图数据,因为和数据库表不同,
                //我们不会更新数据库视图的数据,所以调用AsNoTracking方法来告诉EF Core不用在DbContext中跟踪返回的Iformation实体,可以提高EF Core的运行效率
                var vPersons = dbContext.Information.AsNoTracking().ToList();
                infomation = dbContext.Information.FirstOrDefault(a => a.Icid == 20001);
                Console.WriteLine(infomation.Icid);
                Console.WriteLine(infomation.Ioldtime);
                foreach (var vPerson in vPersons)
                {
                    Console.Write(infomation.Icid + " ");
                    Console.Write(infomation.Iuid + " ");
                    Console.Write(infomation.Ioldtime + " ");
                    Console.Write(infomation.IflowBalance + " ");
                    Console.Write(infomation.IfixBalance + " ");
                    Console.Write(infomation.Iname + " ");
                    Console.WriteLine(infomation.Istatus + " ");
                }
                Console.WriteLine($"Information视图有{vPersons.Count.ToString()}行数据");
                Console.WriteLine(vPersons[0].Icid.ToString());

                #region 计算利息原理
                //时间差值计算利息
                //1. C# DateTime转成MySQL DateTime的字符串:
                //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                //2. MySQL 读出的DateTime转换成C#的DateTime
                DateTime dt1 = Convert.ToDateTime(Convert.ToDateTime(infomation.Ioldtime).ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine(infomation.Ioldtime);                              //查看数据库视图中的旧时间字段
                DateTime dt2 = System.DateTime.Now;                                  //生成新的系统时间
                Console.WriteLine(DateTime.Now);                                     //打印现在时间
                Double Day = dt2.Day - dt1.Day;                                      //天数差值
                Console.WriteLine(Day);
                Double Month = (dt2.Year - dt1.Year) * 12 + (dt2.Month - dt1.Month); //月数差值
                Day = Day * 0.2;
                Console.WriteLine(Day);
                #endregion
            }
            #endregion
            #region 事务操作
            using (var dbContext = new bankContext())
            {
                //事务操作
                using (var transaction = dbContext.Database.BeginTransaction())
                {
                    try
                    {
                        //事务适合同时利用sql语句修改多个数据库表中的字段时,每条都要执行dbContext.SaveChanges(),而多条修改就需要事务提交。否则出错。
                        //第一个更改数据库操作
                        user       = dbContext.Users.FirstOrDefault(a => a.Uid == 10001);
                        user.Uname = "newName" + (new Random().Next(1, 100));
                        dbContext.SaveChanges();
                        //第二个更改数据库操作
                        var id    = 10001;
                        var uname = "王二麻子";
                        var sql   = @"Update Users SET Uname =  {0} WHERE Uid =  {1}";
                        dbContext.Database.ExecuteSqlCommand(sql, uname, id);
                        //保存数据
                        dbContext.SaveChanges();
                        //提交,如果不写并不会报错,但是数据库不会更新
                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        transaction.Rollback();
                    }
                }
            }
            #endregion
        }