从数据库中删除不在平面文件中的记录

时间:2012-11-17 23:51:15

标签: c# asp.net-mvc-3 linq-to-entities

我将记录从文本文件导入到我的数据库中。我有设置检查记录,以确保数据库中没有重复。如果数据库中已有条目,则会跳过该记录。 现在我需要解决的问题是文本文件中没有记录但数据库中有记录。我需要从数据库中删除与文件中的任何记录都不匹配的记录。

    public static void ParseComplaint(string location)
    {

        Console.WriteLine("Parsing.....");
        List<AutoMakeNoEntity> am = DBCacheHelper.GetAllMakes().ToList<AutoMakeNoEntity>();

        using (var reader = new StreamReader(location))
        {
            foreach (string line in File.ReadLines(location))
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }
                var tokens = line.Trim().Split(new char[] { '\t' });

                if (am.Any(c => c.MakeName == tokens[3]))
                {
                    using (RecallsContext context = new RecallsContext())
                    {
                        string tmp = tokens[0];
                        if (!context.complaints.Any(c => c.CMPLID == tmp))
                        {

                            var recalls = new Complaints();

                            recalls.CMPLID = tokens[0];
                            recalls.ODINO = tokens[1];
                            recalls.MFR_NAME = tokens[2];
                            recalls.MAKETXT = tokens[3];
                            recalls.MODELTXT = tokens[4];
                            recalls.YEARTXT = tokens[5];
                            recalls.CRASH = tokens[6];
                            recalls.FAILDATE = tokens[7];
                            recalls.FIRE = tokens[8];
                            recalls.INJURED = tokens[9];
                            recalls.DEATHS = tokens[10];
                            recalls.COMPDESC = tokens[11];
                            recalls.CITY = tokens[12];
                            recalls.STATE = tokens[13];
                            recalls.VIN = tokens[14];
                            recalls.DATEA = tokens[15];
                            recalls.LDATE = tokens[16];
                            recalls.MILES = tokens[17];
                            recalls.OCCURENCES = tokens[18];
                            recalls.CDESCR = tokens[19];
                            recalls.CMPL_TYPE = tokens[20];
                            recalls.POLICE_RPT_YN = tokens[21];
                            recalls.PURCH_DT = tokens[22];
                            recalls.ORIG_OWNER_YN = tokens[23];
                            recalls.ANTI_BRAKES_YN = tokens[24];
                            recalls.CRUISE_CONT_YN = tokens[25];
                            recalls.NUM_CYLS = tokens[26];
                            recalls.DRIVE_TRAIN = tokens[27];
                            recalls.FUEL_SYS = tokens[28];
                            recalls.FUEL_TYPE = tokens[29];
                            recalls.TRANS_TYPE = tokens[30];
                            recalls.VEH_SPEED = tokens[31];
                            recalls.DOT = tokens[32];
                            recalls.TIRE_SIZE = tokens[33];
                            recalls.LOC_OF_TIRE = tokens[34];
                            recalls.TIRE_FAIL_TYPE = tokens[35];
                            recalls.ORIG_EQUIP_YN = tokens[36];
                            recalls.MANUF_DT = tokens[37];
                            recalls.SEAT_TYPE = tokens[38];
                            recalls.RESTRAINT_TYPE = tokens[39];
                            recalls.DEALER_NAME = tokens[40];
                            recalls.DEALER_TEL = tokens[41];
                            recalls.DEALER_CITY = tokens[42];
                            recalls.DEALER_STATE = tokens[43];
                            recalls.DEALER_ZIP = tokens[44];
                            recalls.PROD_TYPE = tokens[45];
                            if (tokens.Length == 47)
                            {
                                recalls.REPAIRED_YN = tokens[46];
                            }

                            context.complaints.Add(recalls);
                            context.SaveChanges();
                            recalls.Dispose();
                        }

                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

  

我需要从数据库中删除与文件中的任何记录都不匹配的记录。

如果您希望数据库与文本文件的内容相匹配,为什么不先将其清空?