5

我正在尝试从分隔的文本文件中读取,但所有内容都以一行和一列的形式返回。

我的连接字符串是

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
    Path.GetDirectoryName(@textBox1txtPath.Text) + ";" + 
    "Extended Properties=\"text;HDR=YES;IMEX=1;Format=Delimited(|)\"");

我的文本文件显示:

ItemNumber|ProductStatus|UPC
0000012|closed|2525

请协助

4

3 回答 3

4

我不知道你到底需要什么,但你可以这样做:

如果你有string str整个文本,你可以做

string[] lines = str.Split('\n');// split it to lines;

然后对于每一行你可以做

string[] cells = line.Split('|');// split a line to cells

如果我们把它提升到一个新的水平,我们可以做到:

public class line
{
  public int ItemNumber { get; set; }
  public string ProductStatus { get; set; }
  public int UPC { get; set; }

  public line(string currLine)
  {
     string[] cells = currLine.Split('|');
     int item;
     if(int.TryParse(cells[0], out item))
     {
        ItemNumber = item;
     }

     ProductStatus = cells[1];

     int upc;
     if (int.TryParse(cells[2], out upc))
     {
        UPC = upc;
     }
  }
}

接着:

string[] lines = str.Substring(str.IndexOf("\n")).Split('\n');// split it to lines;
List<line> tblLines = new List<line>();

foreach(string curr in lines)
{
    tblLines.Add(new line(curr);
}
于 2013-09-11T12:24:33.687 回答
4

好的,所以一种选择是采取不同的方法。考虑以下代码:

// read the entire file and store each line
// as a new element in a string[]
var lines = File.ReadAllLines(pathToFile);

// we can skip the first line because it's
// just headings - if you need the headings
// just grab them off the 0 index
for (int i = 1; i < lines.Length; i++)
{
    var vals = lines[i].Split('|');

    // do something with the vals because
    // they are now in a zero-based array
}

这摆脱了连接字符串的怪异,消除了 Odbc 驱动程序的开销,并大大提高了代码的可读性。

于 2013-09-11T12:27:15.473 回答
3

它就在框架中—— TextFieldParser。不用担心名称空间,它最初是为从 VB6 转换而来的人提供的垫片,但它非常有用。这是一个 SSCCE,它演示了它在许多不同的分隔符中的用途:

class Program
{
    static void Main(string[] args)
    {
        var comma = @"one,""two, yo"",three";
        var tab = "one\ttwo, yo\tthee";
        var random = @"onelol""two, yo""lolthree";

        var parser = CreateParser(comma, ",");
        Console.WriteLine("Parsing " + comma);
        Dump(parser);
        Console.WriteLine();
        parser = CreateParser(tab, "\t");
        Console.WriteLine("Parsing " + tab);
        Dump(parser);
        Console.WriteLine();
        parser = CreateParser(random, "lol");
        Console.WriteLine("Parsing " + random);
        Dump(parser);
        Console.WriteLine();
        Console.ReadLine();
    }

    private static TextFieldParser CreateParser(string value, params string[] delims)
    {
        var parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(ToStream(value));
        parser.Delimiters = delims;
        return parser;
    }

    private static void Dump(TextFieldParser parser)
    {
        while (!parser.EndOfData)
            foreach (var field in parser.ReadFields())
                Console.WriteLine(field);
    }

    static Stream ToStream(string value)
    {
        return new MemoryStream(Encoding.Default.GetBytes(value));
    }
}
于 2015-08-25T15:19:32.103 回答