如何从文本文件/ dat文件中读取数据,使用c#动态创建列并将数据加载到数据表中

时间:2016-07-11 05:36:36

标签: c# winforms visual-studio

如何将文本/ dat文件中的数据加载到c#中的数据表中,这里我需要根据文本文件中的数据动态生成列。

1 个答案:

答案 0 :(得分:0)

     private static System.Data.DataTable SplitColumns()
        {     
            System.Data.DataTable table = new System.Data.DataTable("dataFromFile");
            string file="textfile.txt" ==>Get file which you want to split into columns

            using (StreamReader sr = new StreamReader(file))
            {
                string line;
                int rowsCount = 0;
                while ((line = sr.ReadLine()) != null)
                {

                    string[] data = line.Split(new string[] { "\t"," " }, StringSplitOptions.RemoveEmptyEntries);==>here i'm using the tab delimeter to split the row line
                                                                                                                ==>in the file to columns data,You can use your own delimeter


                    if(table.Columns.Count==0)
                    { 
                    for (int i = 1; i <= data.Length; i++)
                    {

                        table.Columns.AddRange(new DataColumn[] { new DataColumn("col"+(i).ToString(), typeof(string)) });==>here i'm dynamically creating the column headers
                                                                                                                          ==> based on  the strings in the line
                    }
                    }
                    table.Rows.Add();
                    for (int i = 0; i < data.Length; i++)
                    {
                        if (data[i].Contains(" "))
                            data[i] = data[i].Replace(" ", "");
                        if (!data[i].Equals(""))
                            table.Rows[rowsCount][i] = data[i];
                    }
                    rowsCount++;
                }
            }
            return table;
        }