none
无法从已关闭的 TextReader 中读取。 RRS feed

  • 问题

  • private void btnImport_Click(object sender, RoutedEventArgs e)
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "文本文件|*.txt";
                if (ofd.ShowDialog() != true)
                {
                    return;
                }
                string filename = ofd.FileName;
                IEnumerable<string> lines= File.ReadLines(filename);

                foreach (string line in lines)
                {
                    string[] segs = line.Split('|');
                    string name = segs[0];
                    string age = segs[1];
                    SqlHelper.ExecutuNonQuery("insert into T_Customer(Name,Age) values(@name,@age)",
                        new SqlParameter("@name", name), new SqlParameter("@age",Convert.ToInt32(age)));
                }
                MessageBox.Show("成功插入了" + lines.Count() + "条数据");
            }

    我对TextReader 不是很明白。不是用File打开的文件吗?跟TextReader 是什么样的关系啊?请大家指点一下。

    2013年5月13日 6:22

答案

  • 代码中貌似无法看出啥异常,尝试使用File.ReadAllLines

    System.IO.File类貌似没有ReadLines,而且这个方法反射结果如下:

    public static string[] ReadAllLines(string path)

        {

          if (path == null)

            throw new ArgumentNullException("path");

          if (path.Length == 0)

            throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));

          else

            return File.InternalReadAllLines(path, Encoding.UTF8);

        }

    而“InternalReadAllLines”方法继续反射:

    private static string[] InternalReadAllLines(string path, Encoding encoding)

        {

          List<string> list = new List<string>();

          using (StreamReader streamReader = new StreamReader(path, encoding))

          {

            string str;

            while ((str = streamReader.ReadLine()) != null)

              list.Add(str);

          }

          return list.ToArray();

        }

    本质上就是使用了一个文件流读取,而且放入using块中,所以应该会自动关闭的(一旦退出using块)。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年5月13日 6:36

全部回复

  • 代码中貌似无法看出啥异常,尝试使用File.ReadAllLines

    System.IO.File类貌似没有ReadLines,而且这个方法反射结果如下:

    public static string[] ReadAllLines(string path)

        {

          if (path == null)

            throw new ArgumentNullException("path");

          if (path.Length == 0)

            throw new ArgumentException(Environment.GetResourceString("Argument_EmptyPath"));

          else

            return File.InternalReadAllLines(path, Encoding.UTF8);

        }

    而“InternalReadAllLines”方法继续反射:

    private static string[] InternalReadAllLines(string path, Encoding encoding)

        {

          List<string> list = new List<string>();

          using (StreamReader streamReader = new StreamReader(path, encoding))

          {

            string str;

            while ((str = streamReader.ReadLine()) != null)

              list.Add(str);

          }

          return list.ToArray();

        }

    本质上就是使用了一个文件流读取,而且放入using块中,所以应该会自动关闭的(一旦退出using块)。


    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年5月13日 6:36
  • 虽然报错,但是数据库数据插入成功了。
    2013年5月15日 0:35
  • 尝试我上面的代码了吗?用ReadAllLines?

    If you think one reply solves your problem, please mark it as An Answer, if you think someone's reply helps you, please mark it as a Proposed Answer

    Help by clicking:
    Click here to donate your rice to the poor
    Click to Donate
    Click to feed Dogs & Cats


    Found any spamming-senders? Please report at: Spam Report

    2013年5月15日 4:24