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

本文关键字:文件 数据 创建 动态 数据表 加载 文本 dat 读取 使用 | 更新日期: 2023-09-27 17:56:20

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

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

     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;
        }