2013-08-02 97 views
0

我想通过csv上传一系列客户端信息,我在eginning中遇到了一些问题,但是我之前的文章已经回答了,所以我能够开始读取数据它只读取第一行。只是想知道如果有人有任何想法。我已经包括以下csv文件只读第一行

private void btnUpload_Click(object sender, EventArgs e) 
    { 
     //Browse for file 
     OpenFileDialog ofd = new OpenFileDialog(); 
     //Only show .csv files 
     ofd.Filter = "Microsoft Office Excel Comma Separated Values File|*.csv"; 
     DialogResult result = ofd.ShowDialog(); 

     //If the user selects a valid file 
     if (result == DialogResult.OK) 
     { 
      //File is delimited by a comma 
      char[] laClientDelim = { ',' }; 

      //New object for string manipulation 
      objStringManipulation = new StringManipulation(); 

      // Parse the csv file 
      List<string[]> lsClientList = objStringManipulation.parseCSV(ofd.FileName, laClientDelim); 

      foreach (string[] laClient in lsClientList) 
      { 
       //Create new object for manipulating the database 
       objSqlCommands = new SqlCommands("Client", "ClientName"); 



       string[] records = File.ReadAllLines(ofd.FileName); // read the file completely line by line 
       char splitChar = ','; 
       int splitCharCount = 0; 
       int k = 0; 


        string[] fields = records[k].Split(splitChar); // reads all the single values per line. 'splitChar' should be the delimiter. 
        splitCharCount++; 
        if (splitCharCount >= 4 && splitCharCount <= 10) 
        { 
         var stuff = from l in File.ReadLines(ofd.FileName) 
            let x = l.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries) 
               .Skip(1) 
              .Select(s => char.Parse(s)) 
            select new 
            { 
             Client = x, 
             ClientName = x 
            }; 
        } 


        //Inserts the client info into datbase 
       objSqlCommands.sqlCommandInsertorUpdate("INSERT", records[k]);//laClient[0]); 
       k++; 
        //Refreshs the Client table on display from the 
        this.clientTableAdapter.Fill(this.kIIDImplementationCalcDataSet.Client); 

        //MAKE SURE TO ONLY ADD IN CLIENT AND CLIENT NAME 
        //update the view 
        dgvClientlst.Update() ; 





      } 

     } 
    } 
+0

我想你理解一个变量的范围。变量K在for循环中定义,因此在每个循环中一次又一次地实例化为0。在你的循环之外声明K。 – Guanxi

回答

1

代码你的循环是这样的本质:

foreach (string[] laClient in lsClientList) 
{ 
    int k = 0; 
    string[] records = File.ReadAllLines(ofd.FileName); 

    string[] fields = records[k].Split(splitChar); 
    k++; 
} 

你的“K”值永远不会使得过去0每个laClient。您需要为每条线内部循环。

+0

* facepalm *辉煌的感谢,多数民众赞成修复它,noob的错误 – user2546071

0

我知道我的答案不完全是你要找的,但如果你将.csv文件转换为.xls文件,那么你可以很容易地操作它。我多次做过这个,如果你想要,我可以给你提供代码说明。

0

在Jonesy的回答(因为我还不能评论)上扩展,在循环之外声明变量k。它每次都重置为零。

int k = 0; 
foreach (string[] laClient in lsClientList) 
{ 
    string[] records = File.ReadAllLines(ofd.FileName); 

    string[] fields = records[k].Split(splitChar); 
    k++; 
} 
+0

把K外循环修复获得更多的问题不仅仅是第一行,但现在我面临的问题,选择不工作,所以我越来越所有的数据,而不是指定的子字符串 – user2546071

+0

Duh。需要遍历foreach块中的记录和行。正如我已经证明的那样,一个人从来没有经验过犯noob错误。 –