简体   繁体   English

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

[英]Remove records from Database that are not in a flat file

I'm importing records from a text file into my DB. 我正在将记录从文本文件导入数据库。 I have it setup to check the record that I to ensure there is not already a duplicate in the database. 我设置了它来检查记录,以确保数据库中没有重复的记录。 If there is already an entry in the database it skips that record. 如果数据库中已经有一个条目,它将跳过该记录。 Now the issue I need to address is when there is not a record in the text file but there is a record in the database. 现在,我需要解决的问题是文本文件中没有记录,但是数据库中有记录。 I need to remove the record from the database that does not match any records in the file. 我需要从数据库中删除与文件中的任何记录都不匹配的记录。

    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();
                        }

                    }
                }
            }
        }
    }

I need to remove the record from the database that does not match any records in the file. 我需要从数据库中删除与文件中的任何记录都不匹配的记录。

If you want the database to match the contents of the text file, why don't you simply empty it first? 如果您希望数据库与文本文件的内容匹配,为什么不先简单地清空它呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 600K记录的数据库或平面文件? - Database or flat file for 600K records? SSIS 从平面文件过滤记录 - SSIS Filtering records from flat file 从平面文件中将数百万条记录插入SQL Server有哪些陷阱? - What are the pitfalls of inserting millions of records into SQL Server from flat file? 平面文件数据库更新 - Update on flat file database SSIS:如何在C#中从管道定界的平面文件中读取,省略匹配并写入记录 - SSIS: How to read, omit by matching, and write records from pipe delimited flat file in C# 从固定宽度的平面文件到SQL 2000中获取数百万条记录 - Get millions of records from fixed-width flat file to SQL 2000 如何使用SSIS工具从平面文件源中删除空行? - How to remove empty rows using SSIS tools from a flat file source? 如何使用SSIS从单词的列字符串中删除平面文件中的单词 - How to remove words in a flat file from a column string of words using SSIS 如何创建SSIS脚本任务以从平面文件中删除CR LF - How to create SSIS script task to remove the CR LF from a flat file 在没有无限浮动值的情况下将百分比值从平面文件保存到数据库表的正确方法是什么? - What's the proper way to save percentage value from flat file to database table without infinite floating values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM