2013-06-20 33 views
0

根据以下所示的方法将文本文件中的数据添加到数据表中: 假设这些是我在第1个文本文件中使用的行:根据文本文件中的数据在现有列之间添加新列

data1, data2 

1,2 

想这些都是我在第二个文本文件,可以使用行:

data1, data2 

3,4 

我将它们插入到数据表,如下所示:

enter image description here

我已经完成了编码到这一点,它的工作成功。

data1, data0, data2 

5,6,7 

在这种情况下,我需要重新安排我的数据表,如下图所示:当第三个文本文件包含数据,如下图所示,就会出现问题

enter image description here

我尝试过很多方法,但它不断将数据添加到数据表的末尾。有人可以请帮忙吗?我提供我下面的代码:

private DataTable CreateDT1(string name, ListBox list) 
    {    
     DataTable dt = new DataTable();   
     string[] files = new string[list.Items.Count];//text files are taken to a list box 
     for (int x = 0; x < list.Items.Count; x++) 
     { 
      object s = list.Items[x]; 
      files[x] = s.ToString(); 
     }    
     int lineno = 0;     
      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))//i'm filtering some lines containing a given string 
         {           
          if (i == 0) 
          {          
           if (lineno == 0)//first line in the first test file. Eg: data1,data2 
           { 
            string[] split = line.Split(',');//splitting the line adding data to rows 

            int result = split.Length; 
            dt.Rows.Add();           
            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] = split[x]; 
            }                    
           } 
           else 
           { 
            string[] split = line.Split(',');//splitting the other lines in 1st text file and adding data to rows 
            int result = split.Length; 
            dt.Rows.Add(); 
             for (int x = 0; x < result; x++) 
             { 
              dt.Rows[lineno][x] = split[x]; 
             }                    
           }          
          } 

          else 
          {          
           if (count != 0)//for other (2nd, 3rd, ..)text files. This refers to the lines other than the 1st line(5,6,7). First line (data1, data0, data2)of these text files are not accounted here. 
           { 
            string[] split = line.Split(','); 
            int result = split.Length;           
             dt.Rows.Add(); 
             for (int x = 0; x < result; x++) 
             { 
              dt.Rows[lineno][x] = split[x]; 
             }           
           } 

           else //this is the first line of other files 
           { 
            string[] split = line.Split(','); 
            int result = split.Length; 
            if (result + 1 > dt.Columns.Count)//if existing number of columns are less than the split result of the first line, add new columns accordingly. I need help at this point 
            {            
             for (int x = 0; x < result; x++) 
             { 
              Object a = dt.Rows[0][x]; 
              if (split[x] == Convert.ToString(a)) 
              { 
               dt.Rows[0][x] = split[x]; 
              } 
              else 
              { 
               dt.Columns.Add(x.ToString() + split[x]).SetOrdinal(x + 1); 
               //dt.Columns.Add(x.ToString() + split[x]).SetOrdinal(x); 
               dt.Rows[0][x.ToString() + split[x]] = split[x]; 
              }             
             } 

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

           }         

          lineno += 1;         
         } 

        } 

       } 

      }    

     return dt; 
    } 

回答

0

列只能在最后补充说,因为否则它会搞砸现有的INSERT SQL查询。

你不应该关心数据库是如何组织的 - 这就是重点! 稍后只是在您想要的订单查询列。

+0

感谢您的答复。但我不明白如何查询列后添加到数据表的值到想要的顺序。你能不能让我知道如何实施它 –

相关问题