通过 txt 文件批量导入需要批量处理的数据的标识字段

前言

  在一些工作中,可能需要对数据库中的一些数据(批量)进行处理(修改或者查询),而数据的来源是你的同事,换句话说就是这批数据不可能通过某些查询条件查出来,

而这批数据又比较多,比如几百、几千甚至几万个,这种时候如果原有的程序还不支持批量操作,那么如果一个一个的去处理真的是有种想要吐血的感觉!

  所以这里写了一个读取 txt 文件的内容的方法,先将这些数据读取成对应的数据集合,再进行其他处理就方便了很多。

  注:可以直接复制 Excel 表单中的某列数据,粘贴到 txt 文件中。

方法实现:

  这里只是基本的实现,请自行根据需要进行修改!

        public static List<long> GetContentToList(string fileName)
        {
            List<long> list = new List<long>();

            FileStream fs = new FileStream(fileName, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            string line = "";
            while (true)
            {
                line = sr.ReadLine();
                if (line == null)
                {
                    break;
                }
                else
                {
                    line = line.Trim();
                    if (line != "")
                    {
                        list.Add(Convert.ToInt64(line));     //(T)System.ComponentModel.TypeDescriptor.GetConverter(type).ConvertFrom(line)
                    }
                }
            }
            Console.WriteLine($" 读取完毕!共读取数据 {list.Count} 条");

            return list;
        }

方法调用:

            string fileName = "InputFile.txt";
            if (!File.Exists(fileName))
            {
                File.Create(fileName);
            }
            Console.WriteLine(" 加载完成!");
            Console.WriteLine($" 请在 {fileName} 文件中输入你要操作的所有的 URMId(每行一条) !");
            Console.WriteLine("");
            Console.WriteLine(" 输入完成后保存文件,并在这里回车读取文件内容!");
            string s = Console.ReadLine();
            while(true)
            {
                if (s == "")
                {
                    break;
                }
            }

            List<long> urmids = GetContentToList(fileName);

            Console.ReadKey();
原文地址:https://www.cnblogs.com/zhangchaoran/p/10008293.html