2013-03-21 141 views
0

我的問題是關於從文本文件中獲取數據。這是對問題的描述:從文本文件中檢索數據

我的要求是將每行保存在文本文件中。此文本文件由外部來源以未知間隔進行填充。此外,也是最重要的 - 當文件達到100行(例如)時,它會將這些行歸檔到不可訪問的來源中,並清除其內容。

考慮一下監聽器偷看文件並看到它包含10行的情況,監聽器將採取這些行並保存它們。然後,在增量時間(例如1秒)後,再次偷看並看到93行。它也保存了未記錄的83個。 現在,當聽衆第三次偷看時,它看到3行。它明顯地保存了它們,但是注意到有7行(索引爲94-100)沒有被聽衆記錄。

它看起來像是在恆定的三角洲偷看不會確保一個錯過不會發生。

你會建議如何達到這個要求的最好方法?

+0

嘗試與您的節點js c#web應用程序 – 1Mayur 2013-03-21 06:01:20

+0

'FileSystemWatcher'可能會訣竅。 – jordanhill123 2013-03-21 06:14:36

回答

0

這是從文件中取數據線的線,並將其保存到數據庫>>

string line; 
      string[] subLine; 
      DialogResult result = openFileDialog1.ShowDialog(); 
      try 
      { 
       if (result == DialogResult.OK) 
       { 
        string file = openFileDialog1.FileName; 
        StreamReader inputData = new StreamReader(file); 
        const int maxRecords = 500; 
        int actualRecords = 0; 
        while ((line = inputData.ReadLine()) != null) 
        { 
         subLine = line.Split(','); 

         try 
         { 


          string unknown = "unknown Fileld"; 
          con.Open(); 
          // int j=subLine.Length; 
          //for(int i=0;i<subLine.Length;i++) 
          if (actualRecords < maxRecords) 
          { 
           int i = 0; 
           //int j=subLine.Length; 
           //int index=subLine.Length-j; 

           // SQL= "insert into tradeFile values('" + ID + "','" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')"; 

           cmd = new SqlCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con); 
           // cmd = new OleDbCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con); 
           cmd.ExecuteNonQuery(); 
           actualRecords++; 
           // j--; 

          } 
          else 
          { 
           int i = 0; 
           //int j=subLine.Length; 
           //int index=subLine.Length-j; 
           cmd = new SqlCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con); 
           //cmd = new OleDbCommand("insert into tradeFile values('" + subLine[i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + subLine[++i].ToString() + "','" + unknown + "')", con); 
           cmd.ExecuteNonQuery(); 
           actualRecords = 0; 
          } 





         } 
         catch (Exception ex) 
         { 
          MessageBox.Show(ex.Message); 
         } 
         finally 
         { 
          con.Close(); 
         } 
         //counter++; 
        } 
        inputData.Close(); 
       } 
      } 

      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message); 
      } 

希望您能申請進一步的邏輯,它會證明有助於你的代碼。

+0

嗨謝謝你的答案..請注意,這是不適用於問題我intreduced買謝謝無論如何.. – 2013-03-22 10:37:14

0

你可以抱以某種形式收集的線路,如List<string>和驗證List1.Add(line)對集合的容量(100例)使用一段時間或if語句,while(count < capactity)

如果您具有對文件的讀/寫訪問權限,則可以在將行添加到集合後刪除這些行。