Exemple #1
0
        public async Task <ActionResult> ImportDataAsync()
        {
            if (this.Request.Files.Length == 0)
            {
                return(this.RedirectToIndex());
            }
            var file = this.Request.Files.First();

            if (file.FileName.EndsWith(".db", StringComparison.OrdinalIgnoreCase) == false)
            {
                return(this.RedirectToIndex());
            }

            var dbFile = $"data\\import_{Environment.TickCount}.db";

            System.IO.File.WriteAllBytes(dbFile, file.Stream);

            using (var sourceDb = new SqlliteContext(dbFile, false))
            {
                using (var targetDb = new SqlliteContext())
                {
                    var datas = await sourceDb.QuizAnswer.ToArrayAsync();

                    foreach (var data in datas)
                    {
                        if (await targetDb.QuizAnswer.AnyAsync(item => item.Quiz == data.Quiz) == false)
                        {
                            targetDb.QuizAnswer.Add(data);
                        }
                    }
                    var count = await targetDb.SaveChangesAsync();

                    Console.WriteLine($"成功的导入了{count}条数据..");
                }
            }

            System.IO.File.Delete(dbFile);
            return(this.RedirectToIndex());
        }
Exemple #2
0
        /// <summary>
        /// 异步登录
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        private async Task <bool> LoginAsync(string name)
        {
            if (string.IsNullOrEmpty(name) == true)
            {
                return(false);
            }

            using (var db = new SqlliteContext())
            {
                var userIpAddress = await db.UserIpAddress.FindAsync(name);

                if (userIpAddress == null)
                {
                    return(false);
                }

                var clientIp = Request.Headers.TryGet <string>("ClientIpAddress");
                userIpAddress.IpAddress = clientIp;
                await db.SaveChangesAsync();

                Console.WriteLine($"用户登录:{name} ip为{clientIp}");
                return(true);
            }
        }