如何从文本文件中填充数据表?

时间:2017-03-08 00:02:18

标签: c# datatable

我有一个文本文件,我希望用它来填充数据表。我试图看看是否有任何东西插入到表中,但它似乎无法正常工作。我还想知道我是否可以从文本文件中获取列名而不是手动输入。

Player            Team PS GP  Min  FGM  FGA  3M  3A FTM  FTA  OR   TR   AS  ST  TO  BK  PF  DQ   PTS  TC  EJ  FF Sta +/-
acy,quincy        sac SF 59  877  119  214  19  49  50   68  65  188   27  29  27  24 103   0   307   3   0   0  29 -128
adams,jordan      mem SG  2   15    2    6   0   1   3    5   0    2    3   3   2   0   2   0     7   0   0   0   0    4
adams,steven      okl  C 80 2019  261  426   0   0 114  196 218  531   61  42  84  89 223   2   636   2   0   0  80  477
afflalo,arron     nyk SG 71 2359  354  799  91 238 110  131  23  266  145  25  82  10 142   1   909   1   0   0  57 -154

这是我到目前为止所做的:

public static void Main(string[] args)
    {

        DataTable table = new DataTable();
        table.Columns.Add("Player", typeof(string));
        table.Columns.Add("Team", typeof(string));
        table.Columns.Add("PS", typeof(string));
        table.Columns.Add("GP", typeof(int));
        table.Columns.Add("MIN", typeof(string));
        table.Columns.Add("FGM", typeof(string));
        table.Columns.Add("FGA", typeof(string));
        table.Columns.Add("3M", typeof(string));
        table.Columns.Add("3A", typeof(string));
        table.Columns.Add("FTM", typeof(string));
        table.Columns.Add("FTA", typeof(string));
        table.Columns.Add("OR", typeof(string));
        table.Columns.Add("TR", typeof(string));
        table.Columns.Add("AS", typeof(string));
        table.Columns.Add("ST", typeof(string));
        table.Columns.Add("TO", typeof(string));
        table.Columns.Add("BK", typeof(string));
        table.Columns.Add("PF", typeof(string));
        table.Columns.Add("DQ", typeof(string));
        table.Columns.Add("PTS", typeof(string));
        table.Columns.Add("TC", typeof(string));
        table.Columns.Add("EJ", typeof(string));
        table.Columns.Add("FF", typeof(string));
        table.Columns.Add("Sta", typeof(string));
        table.Columns.Add("plusMinus", typeof(string));

        WebClient client = new WebClient();
        Stream stream = client.OpenRead("http://www.dougstats.com/15-16RD.txt");
        StreamReader reader = new StreamReader(stream);
        String content = reader.ReadToEnd();
        Console.WriteLine(content);

        while ((content = reader.ReadToEnd()) != null)
        {
            string[] items = content.Split(' ');
            //make sure it has 3 items
            if (items.Length == 25)
            {
                DataRow row = table.NewRow();
                row["Player"] = items[0];
                row["Team"] = items[1];
                row["PS"] = items[2];
                table.Rows.Add(row);
            }
        }
        foreach (DataRow row in table.Rows)
        {
            Console.WriteLine();
            for (int x = 0; x < table.Columns.Count; x++)
            {
                Console.Write(row[x].ToString() + " ");
            }
        }

0 个答案:

没有答案