1

我需要通过读取现有数据表第一行中的数据来将列添加到现有数据表中。然后需要将数据从文本文件中添加到新列中。例如,请考虑下表作为现有数据表。我在这里提到了第一行中的值(dm1,dm2,..)

图 1

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

图 2

有人可以帮我确定解决方案。提前致谢

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

1 回答 1

-1

如果您将 SQL 查询更改为 SELECT dm1, dm2, dm3, NULL AS dm4, dm5, NULL as dm6, dm7 您将获得一些可以立即使用的填空值

于 2013-06-04T11:35:54.930 回答