如何在C#中存储数据并从文件中检索数据? [英] How to store data and retrieve data from file in C#?

查看:96
本文介绍了如何在C#中存储数据并从文件中检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

员工详细信息,必须存储在文件中并从文件中检索数据c#



我正在尝试使用diff代码。它没有获取数据而是存储为filestroing.employee。





建议使用一些代码吗?



我尝试过:



Employee details,have to store in file and to retrieve data from file in c#

I am trying with diff codes.Its not getting data and but store as filestroing.employee.


Suggest some code to work for this?

What I have tried:

public class BaseEmployee
  {
      public int Id;
      public string Name;
  }

  public class Employee : BaseEmployee
  {
      public string Address;


      public  static  Object Adding()
      {
          Employee employeeDetail = new Employee();
          Console.Write("Employee Id:");
          employeeDetail.Id = int.Parse(Console.ReadLine());

          Console.Write("Employee Name:");
          employeeDetail.Name = Console.ReadLine();
          Console.Write("Employee Address:");
          employeeDetail.Address = Console.ReadLine();
          return employeeDetail;


      }
static void Main(string[] args)
      {
          string pathString = @"c:\SpecifiedFiles\MyNewFile.txt";

          Boolean p = true;

          while (p)
          {
              Console.WriteLine("Employee Record Managemnet system");
              Console.WriteLine("1)Add");
              Console.WriteLine("2)Edit");
              Console.WriteLine("3)Dispaly");
              Console.WriteLine("4)Delete");
              Console.WriteLine("Select operation");


              int option = int.Parse(Console.ReadLine());
              switch(option)
              {
                  case 1:
                      TextWriter tw = new StreamWriter(@"c:\SpecifiedFiles\MyNewFile.txt");
                      tw.Write(Adding());

                      tw.Close();
                       break;

                  case 2:
                      TextReader tr = new StreamReader(@"c:\SpecifiedFiles\MyNewFile.txt");

                      Console.WriteLine(tr.ReadLine());
                      Console.WriteLine(tr.ReadToEnd());
                      tr.Close();
                     break;
               }
          }
       }
      }
  }

推荐答案

你没有提到什么类型的文件。请参阅以下链接,它可以帮助您

编写和阅读文本文件(C#) [ ^ ]
you haven't mentioned what type of files. refer below link it may help you
Writing and reading a text file (C#)[^]


这里更新了案例1,请检查

Here is updated Case 1, please check
using System;
using System.IO;

namespace ConsoleApplication1
{
    class textread
    {
        public class Employee
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
        }

        public static Employee Adding()
        {
            Employee employeeDetail = new Employee();
            Console.Write("Employee Id:");
            employeeDetail.Id = int.Parse(Console.ReadLine());
            Console.Write("Employee Name:");
            employeeDetail.Name = Console.ReadLine();
            Console.Write("Employee Address:");
            employeeDetail.Address = Console.ReadLine();
            return employeeDetail;

        }
        public static void Main(string[] args)
        {
            string pathString = @"D:\temp\MyNewFile.txt";

            Boolean p = true;

            while (p)
            {
                Console.WriteLine("Employee Record Managemnet system");
                Console.WriteLine("1)Add");
                Console.WriteLine("2)Edit");
                Console.WriteLine("3)Dispaly");
                Console.WriteLine("4)Delete");
                Console.WriteLine("Select operation");


                int option = int.Parse(Console.ReadLine());
                switch (option)
                {
                    case 1:
                        TextWriter tw = new StreamWriter(@"D:\temp\MyNewFile.txt");
                        var outObj = Adding();
                        string tempStr = "Id: " + outObj.Id + Environment.NewLine + "Emp Name: " + outObj.Name + Environment.NewLine + "Emp Address: " + outObj.Address;
                        tw.Write(tempStr);
                        tw.Close();
                        break;

                    case 2:
                        TextReader tr = new StreamReader(@"D:\temp\MyNewFile.txt");

                        Console.WriteLine(tr.ReadLine());
                        Console.WriteLine(tr.ReadToEnd());
                        tr.Close();
                        break;
                }
            }
        }
    }

}


这篇关于如何在C#中存储数据并从文件中检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆