【问题标题】:Searching for a single row of data by id number and then printing to console from a csv通过 id 号搜索单行数据,然后从 csv 打印到控制台
【发布时间】:2020-08-10 14:24:05
【问题描述】:

我是 C# 新手,我正在尝试为一个项目构建客户管理系统,在该项目中我想通过 ID 号搜索客户,跳过标题行,然后在用户继续之前将搜索到的行打印到屏幕上.我的 csv 文件是这样设置的:

[ID][Title][firstName][lastName][Gender][DOB]

[0][Mrs][Jane][Doe][Female][1/1/1990]

[1][先生][John][Doe][Male][1/1/1991]

[2][Ms][Sarah][Doe][Female][1/1/2010]

等等……

我感觉我可能使用了错误的inFile.SeekrecordIn 没有得到正确的数据,因为它只在inFile.Seek(0, SeekOrigin.Begin); 上方打印WriteLine

我已经格式化了代码并将你需要的所有东西放在那里,这样更容易调试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            const char DELIM = ',';
            const double END = 000;
            const string FILENAME = "D:\\customers.csv";
            Customer cus = new Customer();
            FileStream inFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(inFile);
            string recordIn;
            string[] fields;
            int idNum;
            Write("  **Type " + END + " To Quit** Enter Customer ID Number> ");
            idNum = Convert.ToInt32(ReadLine());
            while (idNum != END)
            {
                WriteLine("{0,5}{1,10}{2,15}{3,15}{4,15}{5,25}\n", "ID", "Title", "First Name", "Last Name", "Gender", "DOB");
                inFile.Seek(0, SeekOrigin.Begin);
                reader.ReadLine();
                recordIn = reader.ReadLine();
                while (recordIn != null)
                {

                    fields = recordIn.Split(DELIM);

                    cus.ID = Convert.ToInt32(fields[0]);
                    cus.Title = fields[1];
                    cus.FirstName = fields[2];
                    cus.LastName = fields[3];
                    cus.Gender = fields[4];
                    cus.DOB = fields[5];

                    if (cus.ID == idNum)
                        WriteLine("{0,5}{1,10}{2,15}{3,15}{4,15}{5,25}\n", cus.ID, cus.Title, cus.FirstName, cus.LastName, cus.Gender, cus.DOB);
                    recordIn = reader.ReadLine();

                }
                Console.Write("  **Type " + END + " To Quit** Enter Customer ID Number> ");
                idNum = Convert.ToInt32(ReadLine());
            }
            reader.Close();
            inFile.Close();
        }

        public class Customer
        {
            public int idNum { get; set; }
            public string Title { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Gender { get; set; }
            public DateTime DOB { get; set; }
        }


    }
}

任何帮助将不胜感激:))

【问题讨论】:

  • FileStream.Seek 会破坏 StreamReader,因为 StreamReader 有自己的内部缓冲区。因此你不应该重复使用 StreamReader,你应该在调用 FileStream.Seek 之后创建一个新的。
  • 与您 4/21 发布的内容有何不同? stackoverflow.com/questions/61336746/…

标签: c# csv


【解决方案1】:

FileStream.Seek 会中断StreamReader,因为StreamReader 有自己的内部缓冲区。因此你不应该重复使用StreamReader,你应该在调用FileStream.Seek之后创建一个新的

这里有一个不重用 StreamReader 并使用一些 Linq 代码的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        public class Customer
        {
            public int idNum { get; set; }
            public string Title { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Gender { get; set; }
            public DateTime DOB { get; set; }
        }

        static void Main(string[] args)
        {
            const char DELIM = ';';
            const int END = 0;
            const string FILENAME = "D:\\customers.csv";
            using (FileStream inFile = new FileStream(FILENAME, FileMode.Open, FileAccess.Read))
            {
                while (true)
                {
                    Console.WriteLine("  **Type " + END + " To Quit** Enter Customer ID Number> ");
                    var idNum = Convert.ToInt32(Console.ReadLine());
                    if (idNum == END) break;
                    inFile.Position = 0;
                    Console.WriteLine("{0,5}{1,10}{2,15}{3,15}{4,15}{5,25}\n", "ID", "Title", "First Name", "Last Name", "Gender", "DOB");
                    foreach (var customer in GetCustomers(inFile, DELIM).Where(x => x.idNum == idNum))
                    {
                        Console.WriteLine("{0,5}{1,10}{2,15}{3,15}{4,15}{5,25}\n", customer.idNum, customer.Title, customer.FirstName, customer.LastName, customer.Gender, customer.DOB);
                    }
                }
            }
        }

        static IEnumerable<Customer> GetCustomers(Stream input, char separator)
        {
            using (var reader = new StreamReader(input))
            {
                // read header
                reader.ReadLine();

                while (true)
                {
                    var line = reader.ReadLine();
                    if (line == null) yield break;
                    var fields = line.Split(separator);
                    yield return new Customer
                    {
                        idNum = Convert.ToInt32(fields[0]),
                        Title = fields[1],
                        FirstName = fields[2],
                        LastName = fields[3],
                        Gender = fields[4],
                        DOB = Convert.ToDateTime(fields[5])
                    };
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多