C# 通过读取行数据向datatable添加新列

C# 通过读取行数据向datatable添加新列,c#,C#,我需要通过读取现有datatable第一行中的数据向现有datatable添加列。然后需要从文本文件向新列添加数据。例如,请考虑下面给出的表作为现有的DATABATE。我已经提到了第一行的值,dm1,dm2 下面是我需要的数据表。dm4行数据和dm6行数据已添加到此处的两个新列中 有人能帮我找到一个解决方案吗。提前谢谢 private DataTable CreateDT1(string name, ListBox list) { DataTab

我需要通过读取现有datatable第一行中的数据向现有datatable添加列。然后需要从文本文件向新列添加数据。例如,请考虑下面给出的表作为现有的DATABATE。我已经提到了第一行的值,dm1,dm2

下面是我需要的数据表。dm4行数据和dm6行数据已添加到此处的两个新列中

有人能帮我找到一个解决方案吗。提前谢谢

private DataTable CreateDT1(string name, ListBox list)
    {            
        DataTable dt = new DataTable();          
        //there are many files in the list box
        string[] files = new string[list.Items.Count];
        for (int x = 0; x < list.Items.Count; x++)
        {
            object s = list.Items[x];
            files[x] = s.ToString();
        }

        int lineno = 0;
            //for each file in list box
            for (int i = 0; i < files.Length; i++)
            {
                int count = 0;  
                string file = files[i];
                using (System.IO.StreamReader sr = new System.IO.StreamReader(file))
                {                        
                    string line;                        

                    while ((line = sr.ReadLine()) != null)
                    {                         
                        if (line.Contains(name))//filtering the line which contains a specific string"
                        {                                

                            if (i == 0)
                            {                                    
                                if (lineno == 0)//first choosen line of the first text file. 
                                {
                                    //splitting the choosen line
                                    string[] split = line.Split(',');

                                    int result = split.Length;
                                    dt.Rows.Add();
                                    //Based on the number of split length of this row i have added columns
                                    for (int x = 0; x < result; x++)
                                    {
                                        DataColumn dc = new DataColumn(x.ToString(), Type.GetType("System.String"));
                                        dt.Columns.Add(dc);                                            
                                        dt.Rows[lineno][x+1] = split[x];
                                    }

                                }
                                else//splitting and adding other lines of the first text file
                                {
                                    string[] split = line.Split(',');
                                    int result = split.Length;

                                        dt.Rows.Add();
                                        for (int x = 0; x < result; x++)
                                        {
                                            dt.Rows[lineno][x + 1] = split[x];
                                        }                                                                                

                                }                                    
                            }

                            else//adding lines of other text files underneath the same columns
                            {                                    
                                if (count != 0)
                                {
                                    string[] split = line.Split(',');
                                    int result = split.Length;
                                    if (result+1 <= dt.Columns.Count)
                                    {
                                        dt.Rows.Add();
                                        for (int x = 0; x < result; x++)
                                        {
                                            dt.Rows[lineno][x + 1] = split[x];
                                        }

                                    }
                                    else//My issue occurs here when there are additional columns in the other files. Basically when the split length of a line is higher than the previous text files. Then i have to add a new column at the specific positions
                                    {                                            

                                        dt.Rows.Add();
                                        for (int x = 0; x < result; x++)
                                        {
                                            if (split[x]!=dt.Rows[0][x+1])
                                            {
                                            DataColumn dc = new DataColumn(split[x], Type.GetType("System.String"));
                                            dt.Columns.Add(dc);
                                            dt.Rows[lineno][x + 1] = split[x];
                                            }
                                        }

                                    }

                                }

                                else
                                {                                        
                                    lineno -= 1;
                                    count += 1;                                        
                                }

                              }                                

                            lineno += 1;                                
                        }

                    }

                }

            }               

        return dt;
}

如果将SQL查询更改为 选择dm1、dm2、dm3、NULL作为dm4、dm5、NULL作为dm6、dm7
您可以直接使用空白值进行填充

,那么文本文件和数据表是如何关联的呢?表中的第一行属于文本文件中的第一行?@Schmelter Yes第一行是文本中的第一行file@hanzi_ru:分隔符是什么,是否为csv文件,标题行中是否包含列,文本文件中是否包含完整的列?如果没有样本数据,或者没有看到您所做的尝试,您真的很难提供帮助。@Tim:我的编码有点复杂。实际上有不止一个文本文件。我用编码编辑了我的文章。请参考评论以了解。谢谢,她根本没有提到他在使用数据库。总之,这个答案无助于连接来自不同数据源的列,所以-1。