示例#1
0
        static void FileToDb()
        {
            using (var db = new studyinContext())
            {
                var fname = Path.Combine(importPath, "allowToStudy.txt");
                if (File.Exists(fname))
                {
                    var content = File.ReadAllLines(fname);
                    foreach (var line in content)
                    {
                        var fields     = line.Split(',');
                        var identity   = fields[0];
                        var phone      = fields[1];
                        var drivertype = fields[2];
                        DrivingLicenseType enumtype;
                        if (!Enum.TryParse(drivertype, out enumtype))
                        {
                            enumtype = DrivingLicenseType.Unknown;
                        }

                        var drugrelated   = fields[3];
                        var pictureok     = fields[4];
                        var deductedmarks = fields[5];
                        var licensenumber = fields[6];
                        var photofile     = fields[7];
                        var ideducted     = 0;
                        if (!int.TryParse(deductedmarks, out ideducted))
                        {
                            ideducted = 1;
                        }
                        var theuser = db.User.FirstOrDefault(async => async.Identity == identity);
                        if (theuser == null)
                        {
                            try
                            {
                                db.User.Add(new User
                                {
                                    Identity       = identity,
                                    Licensetype    = ((int)enumtype).ToString(),
                                    Drugrelated    = drugrelated,
                                    Syncphone      = phone,
                                    Photostatus    = pictureok,
                                    Drivinglicense = licensenumber,
                                    Deductedmarks  = ideducted,
                                    Photofile      = photofile,
                                    Syncdate       = DateTime.Now
                                });
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine("user {0} sync error{1}.", identity, ex.Message);
                            }
                        }
                        else
                        {
                            Console.WriteLine("user {0} has already existed.", identity);
                        }
                        db.SaveChanges();
                    }
                }
            }
        }